Menu Close

Go – InterfaceAddrs() of net package in go

InterfaceAddrs() function is used to return a list of the system’s unicast interface addresses of net package.

function prototype:

func InterfaceAddrs() ([]Addr, error)

Return type:

InterfaceAddrs() of net package in go returns type Addr interface.

type Addr interface {
	Network() string // name of the network (for example, "tcp", "udp")
	String() string  // string form of address (for example, "192.168.1.1:25", "[2001:db8::1]:80")
}

Example:

package main

import (
    "fmt"
    "net"
    "log"
    )

func main() {
    addrs, err := net.InterfaceAddrs()
    
    if err != nil {
        log.Fatal("Error is: ", err)
    }
    
    for _, addr := range addrs {
        fmt.Println("Network name: ", addr.Network())
        fmt.Println("address: ", addr.String())
    }
}

Output:

Network name:  ip+net
address:  127.0.0.1/8

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.

Contact Us