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
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+13.9k Golang : How to determine if a year is leap year?
+11.1k Golang : Read until certain character to break for loop
+9.1k Golang : Simple histogram example
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+10.9k Golang : How to transmit update file to client by HTTP request example
+46.2k Golang : Read tab delimited file with encoding/csv package
+7.3k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+9.5k Mac OSX : Get a process/daemon status information
+19.5k Golang : Example for DSA(Digital Signature Algorithm) package functions
+7.7k Gogland : Where to put source code files in package directory for rookie
+7k Golang : How to call function inside template with template.FuncMap