Created
March 26, 2014 16:55
-
-
Save jniltinho/9787946 to your computer and use it in GitHub Desktop.
Get My IP Golang
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 | |
/* | |
URL: https://github.com/mccoyst/myip/blob/master/myip.go | |
URL: http://changsijay.com/2013/07/28/golang-get-ip-address/ | |
*/ | |
import ( | |
"net" | |
"os" | |
) | |
func main() { | |
addrs, err := net.InterfaceAddrs() | |
if err != nil { | |
os.Stderr.WriteString("Oops: " + err.Error() + "\n") | |
os.Exit(1) | |
} | |
for _, a := range addrs { | |
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { | |
if ipnet.IP.To4() != nil { | |
os.Stdout.WriteString(ipnet.IP.String() + "\n") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! very simple and powerful!