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
+12.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+18.5k Golang : Set, Get and List environment variables
+15.3k Golang : Get query string value on a POST request
+7k Golang : Find the shortest line of text example
+15.3k Golang : How to get Unix file descriptor for console and file
+10.1k Golang : How to tokenize source code with text/scanner package?
+11.4k Golang : Delay or limit HTTP requests example
+6.8k Golang : Join lines with certain suffix symbol example
+18.6k Golang : Find IP address from string
+6.3k Golang : Selection sort example
+9.8k Golang : Format strings to SEO friendly URL example
+9.1k Golang : Intercept and compare HTTP response code example