Standard library / net

net.InterfaceAddrs()

List local unicast interface addresses without associating them with particular interfaces. This is not a list of reachable public endpoints. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func InterfaceAddrs() ([]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() {  xs := must(net.InterfaceAddrs())  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