Skip to content

Instantly share code, notes, and snippets.

@AHaymond
Forked from jniltinho/myip.go
Created March 24, 2018 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AHaymond/d7b6a0adce15b7d59f14e4dc32f2ccf7 to your computer and use it in GitHub Desktop.
Save AHaymond/d7b6a0adce15b7d59f14e4dc32f2ccf7 to your computer and use it in GitHub Desktop.
Get My IP Golang
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