O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter07/recipe03.
  2. Navigate to the directory.
  3. 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())          }        }
  1. Execute the code by running go run lookup.go in the main Terminal.
  2. You will see the following output:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required