Golang : Resolve domain name to IP4 and IP6 addresses.
In this short tutorial, we will learn how to resolve (find out) the IP, IP4 and maybe IP6 addresses for a domain name. Go's standard library net
package has the ResolveIPAddr function to take care of the low level stuff.
This program below will attempt to find out the given domain name's IP, IP4 and IP6 addresses.
resolveip.go
package main
import (
"net"
"flag"
"fmt"
"os"
)
func main() {
flag.Parse()
domain_name := flag.Arg(0) // get the first argument
ip4address, err := net.ResolveIPAddr("ip4", domain_name)
if err != nil {
fmt.Println("Fail to resolve IP4", err.Error())
os.Exit(1)
}
ip6address, err := net.ResolveIPAddr("ip6", domain_name)
if err != nil {
fmt.Println("Fail to resolve IP6", err.Error())
os.Exit(1)
}
fmt.Printf("Resolved %s to IP4[%s] and IP6[%s] \n", domain_name, ip4address.String(), ip6address.String())
os.Exit(0) // exit gracefully
}
Executing
go run resolveip.go www.google.com
will give the following outputResolved www.google.com to IP4[74.125.130.105] and IP6[2404:6800:4003:c01::6a]
However, let's us try with different domain name and see what's the different
Executing
go run resolveip.go www.amazon.com
will give the following outputFail to resolve IP6 no suitable address found
exit status 1
Hope this tutorial will be helpful to you in learning Go.
See also : Get client IP Address in Go
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6k Golang : Fibonacci number generator examples
+21.5k Golang : simulate tail -f or read last line from log file example
+7.7k Golang : Gaussian blur on image and camera video feed examples
+27.8k Golang : Save map/struct to JSON or XML file
+6.5k Golang : How to convert strange string to JSON with json.MarshalIndent
+46.3k Golang : How to convert JSON string to map and slice
+12.9k Golang : Chunk split or divide a string into smaller chunk example
+18.4k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+8.2k Golang : Play .WAV file from command line
+10.8k Golang : Clean formatting/indenting or pretty print JSON result
+22.2k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+9.2k Golang : How to tokenize source code with text/scanner package?