Skip to content

Instantly share code, notes, and snippets.

@jniltinho
Created March 26, 2014 16:55
Show Gist options
  • Select an option

  • Save jniltinho/9787946 to your computer and use it in GitHub Desktop.

Select an option

Save jniltinho/9787946 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")
}
}
}
}
@imbin

imbin commented May 26, 2021

Copy link
Copy Markdown

Thanks! very simple and powerful!

ghost commented Feb 26, 2022

Copy link
Copy Markdown

牛批的很

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment