Skip to content

Instantly share code, notes, and snippets.

@sonyarianto
Last active December 7, 2018 08:41
Show Gist options
  • Save sonyarianto/38f78b7d8357f0acadc51fc6ceac5356 to your computer and use it in GitHub Desktop.
Save sonyarianto/38f78b7d8357f0acadc51fc6ceac5356 to your computer and use it in GitHub Desktop.
Get local IP address
package main
import (
"fmt"
"log"
"net"
)
func main() {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
fmt.Println(localAddr.IP)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment