Resolving the domain by IP address and vice versa
This recipe will introduce you to how you can translate IP addresses into host addresses and vice versa.
How to do it...
- Open the console and create the folder
chapter07/recipe03
. - Navigate to the directory.
- Create the
lookup.go
file with the following content:
package main import ( "fmt" "net" ) func main() { // Resolve by IP addrs, err := net.LookupAddr("127.0.0.1") if err != nil { panic(err) } for _, addr := range addrs { fmt.Println(addr) } //Resolve by address ips, err := net.LookupIP("localhost") if err != nil { panic(err) } for _, ip := range ips { fmt.Println(ip.String()) } }
- Execute the code by running
go run lookup.go
in the main Terminal. - You will see the following output:

How it works...
The resolution of the...