I Spy Code - GO

System network interfaces

Question:

This do you get information about the system network interfaces in go lang?

Answer:

Interfaces returns a list of the system's network interfaces.

Here is a go lang example that shows how information about each of the systems network interfaces (similar to ifconfig)

Source: (example.go)

package main
 
import (
"fmt"
"net"
)
 
func main() {
   interfaces, _ := net.Interfaces()
   for _, inter := range interfaces {
      fmt.Println("Index :", inter.Index)
      fmt.Println("Name  :", inter.Name)
      fmt.Println("HWaddr:", inter.HardwareAddr)
      fmt.Println("MTU   :", inter.MTU)
      fmt.Println("Flags :", inter.Flags)
      addrs,_ := inter.Addrs()
      for _, ipaddr := range addrs {
         fmt.Println("Addr  :", ipaddr)
      }
      fmt.Println()     
   }
}
 

Output:

$ go run example.go
Index : 1
Name  : lo
HWaddr: 
MTU   : 65536
Flags : up|loopback
Addr  : 127.0.0.1/8
Addr  : ::1/128

Index : 2
Name  : enp3s0
HWaddr: 40:8d:5c:86:5a:14
MTU   : 1500
Flags : up|broadcast|multicast
Addr  : 192.168.2.8/24
Addr  : fe80::dcb3:295c:755b:91bb/64