Golang net.LookupPort() function example

package net

Golang net.LookupPort() function usage example

 package main

 import (
 "fmt"
 "net"
 )

 func main() {
 //-------------------------
 network := "tcp"
 service := "domain"
 portNum, err := net.LookupPort(network, service)

 if err != nil {
 panic(err)
 }

 fmt.Printf("Port number for %s network and %s service is : %d \n", network, service, portNum)

 //-------------------------
 network = "udp"
 service = "syslog"

 portNum, err = net.LookupPort(network, service)

 if err != nil {
 panic(err)
 }

 fmt.Printf("Port number for %s network and %s service is : %d \n", network, service, portNum)
 }

 // NOTES :

 //var porttests = []portTest{
 // {"tcp", "echo", 7, true},
 // {"tcp", "discard", 9, true},
 // {"tcp", "systat", 11, true},
 // {"tcp", "daytime", 13, true},
 // {"tcp", "chargen", 19, true},
 // {"tcp", "ftp-data", 20, true},
 // {"tcp", "ftp", 21, true},
 // {"tcp", "telnet", 23, true},
 // {"tcp", "smtp", 25, true},
 // {"tcp", "time", 37, true},
 // {"tcp", "domain", 53, true},
 // {"tcp", "finger", 79, true},

 // {"udp", "echo", 7, true},
 // {"udp", "tftp", 69, true},
 // {"udp", "bootpc", 68, true},
 // {"udp", "bootps", 67, true},
 // {"udp", "domain", 53, true},
 // {"udp", "ntp", 123, true},
 // {"udp", "snmp", 161, true},
 // {"udp", "syslog", 514, true},

 // {"--badnet--", "zzz", 0, false},
 // {"tcp", "--badport--", 0, false},
 //}

See also :

https://www.socketloop.com/tutorials/golang-find-network-of-an-ip-address

References :

http://golang.org/pkg/net/#LookupPort

Advertisement