Golang net.IP.Equal() function example

package net

Golang net.IP.Equal() function usage example

 package main

 import (
 "fmt"
 "net"
 )

 func main() {

 // google.com ip version 4 and 6 addresses
 // from https://www.socketloop.com/tutorials/golang-resolve-domain-name-ip4-ip6

 ip4 := "74.125.130.105"
 ip4two := "74.125.130.105"

 ip6 := "2404:6800:4003:c01::6a"

 // conver to IP type
 ipAdd4 := net.ParseIP(ip4)
 ipAdd4two := net.ParseIP(ip4two)

 ipAdd6 := net.ParseIP(ip6)

 same := ipAdd4.Equal(ipAdd4two)

 fmt.Println("The ip4 same as ip4two address : ", same)

 same = ipAdd4.Equal(ipAdd6)

 fmt.Println("The ip4 same as ip6 address : ", same)
 }

Output :

The ip4 same as ip4two address : true

The ip4 same as ip6 address : false

References :

http://golang.org/pkg/net/#IP.Equal

https://www.socketloop.com/tutorials/golang-resolve-domain-name-ip4-ip6

Advertisement