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
+9.1k Golang : Handle sub domain with Gin
+9.2k Golang : Create and shuffle deck of cards example
+13.6k Android Studio : Password input and reveal password example
+5.4k Golang : Get S3 or CloudFront object or file information
+16.3k Golang : Find out mime type from bytes in buffer
+13.9k Golang : How to determine if a year is leap year?
+5.1k Golang : Display packages names during compilation
+13.9k Golang : How to check if a file is hidden?
+10.1k Golang : Get login name from environment and prompt for password
+38.1k Golang : Read a text file and replace certain words
+12.1k Golang : Sort and reverse sort a slice of runes
+14.2k Golang : Chunk split or divide a string into smaller chunk example