Last active
June 2, 2018 14:56
-
-
Save chrisneal/b21df627cef21b2a58c5340995f88a96 to your computer and use it in GitHub Desktop.
Fetch the prefered internal IP address
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
func getLocalIP() string { | |
conn, err := net.Dial("udp", "8.8.8.8:80") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer conn.Close() | |
localAddr := conn.LocalAddr().String() | |
idx := strings.LastIndex(localAddr, ":") | |
return localAddr[0:idx] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment