Skip to content

Instantly share code, notes, and snippets.

@BrianLian
Created March 14, 2017 09:17
Show Gist options
  • Save BrianLian/8b020c40ed4c2feb63c60e0232cbdc90 to your computer and use it in GitHub Desktop.
Save BrianLian/8b020c40ed4c2feb63c60e0232cbdc90 to your computer and use it in GitHub Desktop.
Get Interface's IP Address
package main
import "net"
import "fmt"
func main() {
ifaces, err := net.Interfaces()
_ = err // handle err
for _, v := range ifaces {
addrs, err := v.Addrs()
_ = err // handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
}
fmt.Println(v.Name, ":", ip.String())
// process IP address
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment