Golang net.IPConn type LocalAddr() and RemoteAddr() functions example
package net
Golang net.IPConn type functions usage example
NOTE : this example WILL NOT work in Golang's playground. You have to do it in your server
package main
import (
"fmt"
"net"
"os"
)
func main() {
service := "162.243.5.230" // change this
IPAddr, err := net.ResolveIPAddr("ip4", service)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
IPconn, err := net.ListenIP("ip:tcp", IPAddr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Local address : ", IPconn.LocalAddr())
fmt.Println("Remote address : ", IPconn.RemoteAddr())
}
Sample output :
Local address : 162.243.5.230
Remote address :
<nil>
References :
Advertisement
Something interesting
Tutorials
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+14.6k Golang : How to get URL port?
+8.5k Linux/Unix : fatal: the Postfix mail system is already running
+9.2k Golang : does not implement flag.Value (missing Set method)
+19.9k Golang : Count JSON objects and convert to slice/array
+6k Golang : Experimenting with the Rejang script
+16.3k Golang : Loop each day of the current month example
+26k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+7k Golang : How to call function inside template with template.FuncMap
+8.6k Golang : Convert(cast) []byte to io.Reader type
+14.4k Golang : How to convert a number to words
+12.7k Golang : Remove or trim extra comma from CSV