I Spy Code - GO

Get host name from ip address

Question:

How to do you get a hostname (domainname) for a IP address in go lang?

Answer:

LookupAddr performs a reverse lookup for the given address, returning a list of names mapping to that address.

Here is a go lang example that shows how to get a host name for a given ip address.

Source: (example.go)

package main
 
import (
  "fmt"
  "net"
)
 
func main() {
   host, err := net.LookupAddr("50.62.227.1")
   if err == nil {
      fmt.Println(host)
   }
}
 

Output:

$ go run example.go
[ispycode.com.]

References:

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