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
+27.6k PHP : Convert(cast) string to bigInt
+26.4k Golang : Get executable name behind process ID example
+7.4k Golang : Word limiter example
+19.6k Golang : Set or Add HTTP Request Headers
+5.9k Facebook : How to force facebook to scrape latest URL link data?
+20k Golang : How to run your code only once with sync.Once object
+9.3k Golang : Temperatures conversion example
+34.1k Golang : Create x509 certificate, private and public keys
+36.4k Golang : Convert date or time stamp from string to time.Time type
+14.4k Golang : On enumeration
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+13.5k Golang : How to get year, month and day?