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
+14.6k Golang : Convert(cast) int to float example
+7.1k Golang : Squaring elements in array
+8.9k Golang : Sort lines of text example
+6.1k nginx : force all pages to be SSL
+5.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+10.6k Golang : Simple File Server
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+21.8k Golang : How to reverse slice or array elements order
+7.5k Golang : Shuffle strings array
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+16.5k Golang : Execute terminal command to remote machine example