Golang net.IPv4() function example
package net
Golang net.IPv4() function example
package main
import (
"fmt"
"net"
)
func main() {
ipAddress := net.IPv4(192, 168, 0, 1)
fmt.Println(ipAddress)
//sanity check
if ipAddress.To4() == nil {
fmt.Printf("%v is not a valid IPv4 address\n", ipAddress)
} else {
fmt.Println("ipAddress is valid IPv4 address")
}
}
Output :
192.168.0.1
ipAddress is valid IPv4 address
Reference :
http://golang.org/pkg/net/#IPv4
https://www.socketloop.com/tutorials/golang-check-if-ip-address-is-version-4-or-6
Advertisement
Something interesting
Tutorials
+48.1k Golang : How to convert JSON string to map and slice
+5.7k Fix yum-complete-transaction error
+8.8k Golang : Random integer with rand.Seed() within a given range
+7.5k Golang : Handling Yes No Quit query input
+30k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+15.2k Golang : How to check if IP address is in range
+11.6k Golang : Convert(cast) float to int
+9.6k Golang : Validate IPv6 example
+46.4k Golang : Encode image to base64 example
+8.8k Golang : Get final balance from bit coin address example
+8.2k Golang : Add build version and other information in executables
+15.6k Golang : Force download file example