conn, err := net.Dial("tcp", "example.com:80") if err != nil { // handle error } err = conn.SetReadDeadline(time.Now().Add(10 * time.Second)) if err != nil { // handle error } // read from the connection
conn, err := net.Dial("udp", "example.com:1234") if err != nil { // handle error } err = conn.SetWriteDeadline(time.Now().Add(5 * time.Second)) if err != nil { // handle error } // write data to the connectionIn this example, we connect to example.com on port 1234 using UDP and set a write deadline of 5 seconds. If the connection cannot send any data within 5 seconds, the write operation will be cancelled and an error will be returned. These examples use the Go net package.