Created
September 1, 2020 13:16
-
-
Save dansku/f754df9cf417c0cf064709aa47d6853d to your computer and use it in GitHub Desktop.
List all network interfaces in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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