Created
October 13, 2014 22:15
-
-
Save rooty/36cd9eb6941ae95974a4 to your computer and use it in GitHub Desktop.
Get local IP addresses
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
Use net.InterfaceAddrs(). | |
addrs, err := net.InterfaceAddrs() | |
if err != nil { | |
panic(err) | |
} | |
for i, addr := range addrs { | |
fmt.Printf("%d %v\n", i, addr) | |
} | |
If you want to know interface names too, use net.Interfaces() to get a list of interfaces first. | |
list, err := net.Interfaces() | |
if err != nil { | |
panic(err) | |
} | |
for i, iface := range list { | |
fmt.Printf("%d name=%s %v\n", i, iface.Name, iface) | |
addrs, err := iface.Addrs() | |
if err != nil { | |
panic(err) | |
} | |
for j, addr := range addrs { | |
fmt.Printf(" %d %v\n", j, addr) | |
} | |
} | |
#pkg~net |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment