Menu Close

Go – IsLinkLocalUnicast() of net package in go

In this tutorial, we are going to learn about IsLinkLocalUnicast() function of net package in go with example.

IsLinkLocalUnicast() function is used to check whether ip is a link-local unicast address or not of net package in go.

function prototype:

func (ip IP) IsLinkLocalUnicast() bool

Function returns true if ip is a link-local unicast address else false.

Example:

package main

import (
	"fmt"
	"net"
)

func main() {
    ipv4_ip := net.ParseIP("169.254.0.0")
    is_link_local_unicast := ipv4_ip.IsLinkLocalUnicast()
    
    if is_link_local_unicast == true {
        fmt.Println("169.254.0.0 is a link-local unicast address.")
    } else {
        fmt.Println("169.254.0.0 is not a link-local unicast address.")
    }
    
    ipv4_ip = net.ParseIP("224.0.0.0")
    is_link_local_unicast = ipv4_ip.IsLinkLocalUnicast()
    
    if is_link_local_unicast == true {
        fmt.Println("224.0.0.0 is a link-local unicast address.")
    } else {
        fmt.Println("224.0.0.0 is not a link-local unicast address.")
    }
    
    ipv6_ip := net.ParseIP("ff02::2")
    is_link_local_unicast = ipv6_ip.IsLinkLocalUnicast()
    
    if is_link_local_unicast == true {
        fmt.Println("ff02::2 is a link-local unicast address.")
    } else {
        fmt.Println("ff02::2 is not a link-local unicast address.")
    }
    
    ipv6_ip = net.ParseIP("fe80::")
    is_link_local_unicast = ipv6_ip.IsLinkLocalUnicast()
    
    if is_link_local_unicast == true {
        fmt.Println("fe80:: is a link-local unicast address.")
    } else {
        fmt.Println("fe80:: is not a link-local unicast address.")
    }
}

Output:

169.254.0.0 is a link-local unicast address.
224.0.0.0 is not a link-local unicast address.
ff02::2 is not a link-local unicast address.
fe80:: is a link-local unicast address.

What is Link-Local Unicast Address ?

A link-local address is a unicast address that is confined to a single link and a single subnet. Link-local addresses only need to be unique on the link (subnet) and do not need to be unique beyond the link. Therefore, routers do not forward packets with a link-local address

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

https://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 *