Standard library / net
net.InterfaceByName()
Look up a network interface by its local name; do not assume a particular name is portable. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func InterfaceByName(name string) (*Interface, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| name | string | Argument of the declared type; see example and description. |
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") } x := must(net.InterfaceByName(loop.Name)) fmt.Println(x.Index == loop.Index)}
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) }}