Created
March 14, 2017 09:17
-
-
Save IS-BrianLian/8b020c40ed4c2feb63c60e0232cbdc90 to your computer and use it in GitHub Desktop.
Get Interface's IP Address
This file contains hidden or 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 "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