Standard library / net

net.Interface.Addrs()

List unicast addresses assigned to this interface. The returned list depends on the system configuration. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (ifi *Interface) Addrs() ([]Addr, error)

Return Value

([]Addr, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net")
func main() {  all := must(net.Interfaces())  var loop net.Interface  for _, x := range all {    if x.Flags&net.FlagLoopback != 0 {      loop = x      break    }  }  if loop.Index == 0 {    panic("no loopback interface")  }  xs := must(loop.Addrs())  ok := false  for _, x := range xs {    if ip, _, e := net.ParseCIDR(x.String()); e == nil && ip.IsLoopback() {      ok = true    }  }  fmt.Println(ok)}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference