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
+4.9k JQuery : Calling a function inside Jquery(document) block
+10.6k Golang : ISO8601 Duration Parser example
+11.1k Golang : Roll the dice example
+9.4k Golang : Find the length of big.Int variable example
+25.3k Golang : Get current file path of a file or executable
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+22.6k Generate checksum for a file in Go
+4.9k Python : Find out the variable type and determine the type with simple test
+7.6k Javascript : Push notifications to browser with Push.js
+14.1k Golang : Check if a file exist or not
+6.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+26.7k Golang : How to check if a connection to database is still alive ?