Skip to content

Instantly share code, notes, and snippets.

@chrisneal
Last active June 2, 2018 14:56
Show Gist options
  • Save chrisneal/b21df627cef21b2a58c5340995f88a96 to your computer and use it in GitHub Desktop.
Save chrisneal/b21df627cef21b2a58c5340995f88a96 to your computer and use it in GitHub Desktop.
Fetch the prefered internal IP address
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