Golang net.ParseIP() and DefaultMask() functions example
package net
Golang net.ParseIP() function usage example
package main
import (
"fmt"
"net"
"os"
)
func main() {
addr := net.ParseIP("170.149.172.130")
if addr == nil {
fmt.Println("Invalid address")
os.Exit(1)
}
mask := addr.DefaultMask() // only works on IPv4 address
network := addr.Mask(mask)
fmt.Printf("Address : %s \nNetwork : %s \n", addr.String(), network.String())
}
Output :
Address : 170.149.172.130
Network : 170.149.0.0
References :
https://www.socketloop.com/tutorials/golang-find-network-of-an-ip-address
Advertisement
Something interesting
Tutorials
+21.2k Golang : How to get time zone and load different time zone?
+8.3k Golang: Prevent over writing file with md5 hash
+39k Golang : How to iterate over a []string(array)
+6k Golang : Compound interest over time example
+13.1k Golang : List objects in AWS S3 bucket
+12.8k Golang : Listen and Serve on sub domain example
+36.5k Golang : Validate IP address
+7.8k Golang : Lock executable to a specific machine with unique hash of the machine
+11.3k Golang : Byte format example
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+7.4k Golang : Check to see if *File is a file or directory
+7k Golang : Takes a plural word and makes it singular