Created
November 12, 2021 09:04
-
-
Save wilkice/426e2b4aeb19b8881647f7b28f5b8a38 to your computer and use it in GitHub Desktop.
[get outbound ip] #go
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
| import ( | |
| "log" | |
| "net" | |
| ) | |
| // Get preferred outbound ip of this machine | |
| func GetOutboundIP() net.IP { | |
| conn, err := net.Dial("udp", "8.8.8.8:80") | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer conn.Close() | |
| localAddr := conn.LocalAddr().(*net.UDPAddr) | |
| return localAddr.IP | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment