Standard library / net
net.Interfaces()
List network interfaces and their metadata. The inventory is machine-specific and may change; select the interface using flags rather than assuming a name. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func Interfaces() ([]Interface, error)Return Value
([]Interface, 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") } fmt.Println(loop.Index > 0)}
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) }}