Menu Close

Go – IsLinkLocalMulticast() of net package in go

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

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

function prototype:

func (ip IP) IsLinkLocalMulticast() bool

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

Example:

package main

import (
	"fmt"
	"net"
)

func main() {
    ipv4_ip := net.ParseIP("224.0.0.0")
    is_link_local_multicast := ipv4_ip.IsLinkLocalMulticast()
    
    if is_link_local_multicast == true {
        fmt.Println("224.0.0.0 is a link-local multicast address.")
    } else {
        fmt.Println("224.0.0.0 is not a link-local multicast address.")
    }
    
    ipv4_ip = net.ParseIP("255.254.0.0")
    is_link_local_multicast = ipv4_ip.IsLinkLocalMulticast()
    
    if is_link_local_multicast == true {
        fmt.Println("255.254.0.0 is a link-local multicast address.")
    } else {
        fmt.Println("255.254.0.0 is not a link-local multicast address.")
    }
    
    ipv6_ip := net.ParseIP("ff02::2")
    is_link_local_multicast = ipv6_ip.IsLinkLocalMulticast()
    
    if is_link_local_multicast == true {
        fmt.Println("ff02::2 is a link-local multicast address.")
    } else {
        fmt.Println("ff02::2 is not a link-local multicast address.")
    }
    
    ipv6_ip = net.ParseIP("fe80::")
    is_link_local_multicast = ipv6_ip.IsLinkLocalMulticast()
    
    if is_link_local_multicast == true {
        fmt.Println("fe80:: is a link-local multicast address.")
    } else {
        fmt.Println("fe80:: is not a link-local multicast address.")
    }
}

Output:

224.0.0.0 is a link-local multicast address.
255.254.0.0 is not a link-local multicast address.
ff02::2 is a link-local multicast address.
fe80:: is not a link-local multicast address.

What is link-local address ?

A link-local address is required on each physical interface. A link-local address is used for addressing on a single link for automatic address configuration, neighbour discovery or in the absence of routers. A link-local address may also be used to communicate with other nodes on the same link and It’s assigned automatically.

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 *