Golang net.Dial() function examples

package net

Golang net.Dial() function usage examples

Example 1:

 doc_socket, err := net.Dial("unix", "/var/run/docker.sock")
 defer doc_socket.Close()

Example 2:

 package main

 import (
 "fmt"
 "net"
 )

 func main() {
 hostName := "socketloop.com"
 portNum := "80"

 conn, err := net.Dial("tcp", hostName + ":" + portNum)

 if err != nil {
 fmt.Println(err)
 return
 }

 fmt.Printf("Connection established between %s and localhost.\n", hostName)
 fmt.Printf("Remote Address : %s \n", conn.RemoteAddr().String())
 fmt.Printf("Local Address : %s \n", conn.LocalAddr().String())

 }

Sample output :

Connection established between socketloop.com and localhost.

Remote Address : 162.243.5.230:80

Local Address : 192.168.1.64:51810

References:

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

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

Advertisement