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
+26.8k Golang : Find files by extension
+17.9k Golang : How to make a file read only and set it to writable again?
+13.4k Golang : Generate Code128 barcode
+6.5k Elasticsearch : Shutdown a local node
+5.5k Golang : Stop goroutine without channel
+29.9k Golang : How to get HTTP request header information?
+30.5k Get client IP Address in Go
+23.9k Golang : Use regular expression to validate domain name
+22.2k Golang : Securing password with salt
+12.6k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+15.2k Golang : Save(pipe) HTTP response into a file
+9.4k Golang : Find the length of big.Int variable example