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