I Spy Code - GO

Get ipaddress from hostname

Question:

How to do you get an ipaddress for using a host name in go lang?

Answer:

LookupIP looks up host using the local resolver. It returns an array of that host's IPv4 and IPv6 addresses.

Here is a go lang example that shows how get an ipaddress for a given host name.

Source: (example.go)

package main
 
import (
   "net"
   "fmt"
)
 
func main() {
   addr,err := net.LookupIP("ispycode.com")
   if err != nil {
      fmt.Println("Unknown host")
   } else {
      fmt.Println("IP address: ", addr)
   }
}
 

Output:

$ go run example.go
IP address:  [50.62.227.1]

References:

https://golang.org/pkg/net/#LookupHost