package main import ( "fmt" "net" ) func main() { conn, err := net.Dial("tcp", "google.com:80") if err != nil { fmt.Println("Error connecting to server: ", err) return } defer conn.Close() fmt.Println("Successfully established connection with Google") }
package main import ( "net" "time" ) func main() { dialer := &net.Dialer{ Timeout: time.Second * 5, KeepAlive: time.Second * 10, } conn, err := dialer.Dial("tcp", "google.com:80") if err != nil { // handle error } defer conn.Close() // use the connection }In this example, we are creating a custom dialer object with a 5-second timeout and 10-second keep-alive interval. We then use this custom dialer to establish a connection with the Google server. Overall, the net.Dialer.Dial method is a powerful tool for establishing network connections in Go. It is a part of the standard Go net package and is widely used in creating networked applications.