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
+7k Golang : Find the shortest line of text example
+13.4k Golang : Read from buffered reader until specific number of bytes
+20.7k Android Studio : AlertDialog and EditText to get user string input example
+12.1k Golang : convert(cast) string to integer value
+24.5k Golang : Change file read or write permission example
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+8.3k Golang : Auto-generate reply email with text/template package
+13.2k Golang : Convert(cast) int to int64
+11.5k Golang : Handle API query by curl with Gorilla Queries example
+6.8k Swift : substringWithRange() function example
+8.2k Golang : Reverse text lines or flip line order example