Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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")
}
}
}
}
@wuranbo
Copy link

wuranbo commented Aug 11, 2015

just thank you very much.

@bevanz
Copy link

bevanz commented May 8, 2016

Thanks, This helps a lot!

Copy link

ghost commented Jun 5, 2016

i want explanation of this code

Copy link

ghost commented Nov 29, 2016

Thanks, This helps a lot!

@Konstantin8105
Copy link

Konstantin8105 commented Apr 14, 2017

Thanks

@Demitroi
Copy link

Demitroi commented Jul 7, 2017

Thanks, good work

Copy link

ghost commented Oct 31, 2017

Might be easier to do something like this:

package main

import "net"

func main() {
    conn, err := net.Dial("udp", "8.8.8.8:80")
    // handle err...

     defer conn.Close()
     localAddr := conn.LocalAddr().(*net.UDPAddr)
}

This will give you the primary IP address used by your system (great for machines that have multiple ips, interfaces etc).

Copy link

ghost commented Oct 31, 2017

Also, you can use the netFlags to determine the status and capabilities of that interface which could be very useful.

@edwardstudy
Copy link

edwardstudy commented Apr 12, 2018

@rucuriousyet Could I use 1.2.3.4:1 to dial udp.

@pashok2398
Copy link

pashok2398 commented Dec 3, 2018

@jniltinho thanks it worked for me :)
@rucuriousyet in my case i have 2 addresses on same interface i think your solution will get only one of them....
Can someone explain line 21, didnt got the a.(*net.IPNet).
Thanks

@g10guang
Copy link

g10guang commented Dec 6, 2018

Might be easier to do something like this:

package main

import "net"

func main() {
    conn, err := net.Dial("udp", "8.8.8.8:80")
    // handle err...

     defer conn.Close()
     localAddr := conn.LocalAddr().(*net.UDPAddr)
}

This will give you the primary IP address used by your system (great for machines that have multiple ips, interfaces etc).

China cannot access google dns 8.8.8.8

@shilangyu
Copy link

shilangyu commented Sep 8, 2019

Might be easier to do something like this:

package main

import "net"

func main() {
    conn, err := net.Dial("udp", "8.8.8.8:80")
    // handle err...

     defer conn.Close()
     localAddr := conn.LocalAddr().(*net.UDPAddr)
}

This will give you the primary IP address used by your system (great for machines that have multiple ips, interfaces etc).

China cannot access google dns 8.8.8.8

Does not matter, its a UDP connection therefore there is no handshake, works offline

@west0706
Copy link

west0706 commented Jul 16, 2020

Thanks! very simple and powerful!

@imbin
Copy link

imbin commented May 26, 2021

Thanks! very simple and powerful!

@ILoveEverything
Copy link

ILoveEverything commented Feb 26, 2022

牛批的很

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