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
+5k Golang : Constant and variable names in native language
+17.8k Golang : Iterate linked list example
+7.5k Golang : Rename part of filename
+7.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+7k Golang : Levenshtein distance example
+8.6k Golang : Convert(cast) []byte to io.Reader type
+8.6k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+28k Golang : Move file to another directory
+13.8k Golang : unknown escape sequence error
+8.8k Golang : Executing and evaluating nested loop in html template
+31.5k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions