Menu Close

Go – Validate IPv4 and IPv6 address in go

Here, we will learn how to validate IPv4 and IPv6 address in go. We can do it by using either ParseIP() or IPv4() function in net package.

Basically, we use ParseIP() or IPv4() function of net package to validate IPv4 and IPv6 address in go.

ParseIP() function prototype:

func ParseIP(s string) IP

Parameters:
s is the IP address in string format like "10.10.10.1"

In function call, It would be: net.ParseIP(s)

IPv4() function prototype:

func IPv4(a, b, c, d byte) IP

Parameters:
a, b, c, d are IP address number separated by dot, like suppose IP address is "192.168.10.1" then a = 192, b = 168, c = 10, d = 1.

In function call, it would be: net.IPv4(192, 168, 10, 1)

ParseIP() function return value:

  • ParseIP() function returns nil if IP address is not valid.
  • ParseIP() function returns address in IP format if there is a valid IP address

IPv4() function return value:

  • IPv4() function returns error message if IP address is not valid
  • IPv4() function returns address in IP format if there is a valid IP address

ParseIP() function with example and code:

package main
import (
    "fmt"
    "net"
    "reflect"
)

func main() {

    // Validate IPv4 address with ParseIP() function
    ipv4 := "10.1.1.0"
    x := net.ParseIP(ipv4)
    
    if x == nil {
        fmt.Println("Invalid IP address: ", ipv4)
    } else {
        fmt.Println("IPv4 address: ", x)
        fmt.Println("Type: ", reflect.TypeOf(x))
    }
    
    // Validate invalid IPv4 address with ParseIP() function
    fmt.Println()
    
    ipv4 = "10.10.0"
    x = net.ParseIP(ipv4)
    
    if x == nil {
        fmt.Println("Invalid IP address: ", ipv4)
    } else {
        fmt.Println("IPv4 address: ", x)
    }
    
    // Validate IPv6 address with ParseIP() function
    fmt.Println()
    
    ipv6 := "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
    x = net.ParseIP(ipv6)
    
    if x == nil {
        fmt.Println("Invalid IP address: ", ipv6)
    } else {
        fmt.Println("IPv6 address: ", x)
        fmt.Println("Type: ", reflect.TypeOf(x))
    }
}

Output:

IPv4 address:  10.1.1.0
Type:  net.IP

Invalid IP address:  10.10.0

IPv6 address:  2001:db8:85a3::8a2e:370:7334
Type:  net.IP

IPv4() function with example and code:

package main
import (
    "fmt"
    "net"
    "reflect"
)

func main() {

    // Validate IPv4 address with IPv4() function
    x := net.IPv4(10, 1, 1, 0)
    
    fmt.Println("IPv4 address: ", x)
    fmt.Println("Type: ", reflect.TypeOf(x))
}

Output:

IPv4 address:  10.1.1.0
Type:  net.IP
package main
import (
    "fmt"
    "net"
    "reflect"
)

func main() {

    // Validate invalid IPv4 address with IPv4() function
    x := net.IPv4(10, 1, 1)
    
    fmt.Println("IPv4 address: ", x)
    fmt.Println("Type: ", reflect.TypeOf(x))
}

Output:

# command-line-arguments
./main.go:11:18: not enough arguments in call to net.IPv4
	have (number, number, number)
	want (byte, byte, byte, byte)

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/


References: https://golang.org/pkg/

Posted in golang, net, packages

Leave a Reply

Your email address will not be published. Required fields are marked *