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
+12.4k Golang : Extract part of string with regular expression
+6.5k Unix/Linux : How to get own IP address ?
+12.3k Golang : Display list of countries and ISO codes
+16.5k Golang : Get IP addresses of a domain name
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+21.8k Golang : Convert string slice to struct and access with reflect example
+12.3k Golang : List running EC2 instances and descriptions
+4.7k Chrome : How to block socketloop.com links in Google SERP?
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+14.5k Golang : Rename directory
+8k Golang : Sort words with first uppercase letter
+6.6k Golang : How to validate ISBN?