THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial explains how to get a local IP address in Golang.
Usually, When you retrieved the IP address using golang API’s, gives a loopback address 12.0.0.1 configured in the/etc/hosts
file, and does not give a local public outbound IP address such as 132.121.xxx.xxx.
Here is an example
package main
import (
"fmt"
"log"
"net"
)
// Local address of the running system
func getLocalAddress() net.IP {
con, error := net.Dial("udp", "8.8.8.8:80")
if error != nil {
log.Fatal(error )
}
defer con.Close()
localAddress := con.LocalAddr().(*net.UDPAddr)
return localAddress.IP
}
func main() {
ipString := GetOutboundIP()
fmt.Println(ipString)
}
🧮 Tags
Recent posts
Puppeteer Login Test example How to convert Double to Integer or Integer to double in Dart| Flutter By Example Best ways to fix 504 gateway time out in nodejs Applications Fix for Error No configuration provided for scss Multiple ways to List containers in a Docker with examplesRelated posts