Skip to content

Instantly share code, notes, and snippets.

@dansku
Created September 1, 2020 13:16
Show Gist options
  • Save dansku/f754df9cf417c0cf064709aa47d6853d to your computer and use it in GitHub Desktop.
Save dansku/f754df9cf417c0cf064709aa47d6853d to your computer and use it in GitHub Desktop.
List all network interfaces in go
package main
import (
"fmt"
"net"
)
func localAddresses() {
ifaces, err := net.Interfaces()
if err != nil {
fmt.Print(fmt.Errorf("localAddresses: %+v\n", err.Error()))
return
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
fmt.Print(fmt.Errorf("localAddresses: %+v\n", err.Error()))
}
for _, a := range addrs {
fmt.Printf("%v - %v\n", i.Name, a)
}
}
}
func main() {
fmt.Println("Starting")
localAddresses()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment