Standard library / net

net.InterfaceByIndex()

Resolve the interface with a positive index; indices are local to the operating system and can change. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func InterfaceByIndex(index int) (*Interface, error)

Parameters & Attributes

NameTypeDescription
indexintArgument 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.InterfaceByIndex(loop.Index))  fmt.Println(x.Name == loop.Name)}
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