I Spy Code - GO

Get network from a hostname

Here is a go lang example that shows how get the network from a host name:

Source: (example.go)

import (
   "fmt"
   "net"
)
 
func main() {
   hostname := "www.boston.com"
 
   IPAddr, _ := net.ResolveIPAddr("ip", hostname)
   addr := net.ParseIP(IPAddr.String())
 
   // DefaultMask returns the default IP mask for the IP address ip.
   mask := addr.DefaultMask()
 
   // Mask returns the result of masking the IP address ip with mask.
   network := addr.Mask(mask)
 
   fmt.Println("Hostname:", hostname)
   fmt.Println("Address :", addr.String())
   fmt.Println("Network :", network)
}
 

Output:

$ go run example.go
Hostname: www.boston.com
Address : 151.101.0.153
Network : 151.101.0.0