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 :

http://golang.org/pkg/net/#IPConn

Advertisement