Golang net.IP.Mask(), String(), To16() and To4() functions example
package net
Golang net.IP.Mask(), String(), To16() and To4() functions usage example
package main
import (
"fmt"
"net"
"os"
)
func main() {
hostname := "www.golang.org"
IPAddr, err := net.ResolveIPAddr("ip", hostname)
if err != nil {
fmt.Println("Error in resolving IP")
os.Exit(1)
}
addr := net.ParseIP(IPAddr.String())
if addr == nil {
fmt.Println("Invalid address")
os.Exit(1)
}
mask := addr.DefaultMask()
network := addr.Mask(mask)
fmt.Printf("Address : %s \nNetwork : %s \n", addr.String(), network.String())
// convert addr IP address to 16-byte
十六IP := addr.To16()
fmt.Printf("16 byte representation : %s\n", 十六IP.String())
// convert network IP address to 16-byte
四IP := addr.To4()
fmt.Printf("4 byte representation : %s\n", 四IP.String())
}
Sample output :
Address : 74.125.200.141
Network : 74.0.0.0
16 byte representation : 74.125.200.141
4 byte representation : 74.125.200.141
References :
http://golang.org/pkg/net/#IP.Mask
http://golang.org/pkg/net/#IP.String
Advertisement
Something interesting
Tutorials
+5.6k Swift : Get substring with rangeOfString() function example
+7.3k Golang : File system scanning
+12.4k Elastic Search : Return all records (higher than default 10)
+10.4k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+9.5k Mac OSX : Get a process/daemon status information
+16.3k Golang :Trim white spaces from a string
+5.2k Golang : Issue HTTP commands to server and port example
+11.5k Golang : Handle API query by curl with Gorilla Queries example
+8.8k Golang : HTTP Routing with Goji example
+8.2k Prevent Write failed: Broken pipe problem during ssh session with screen command
+6.6k Golang : Warp text string by number of characters or runes example
+18.8k Golang : How to make function callback or pass value from function as parameter?