1> Cerise Limón..:
Use net.Dialer with either timeout or deadline field set.
d := net.Dialer{Timeout: timeout} conn, err := d.Dial("tcp", addr) if err != nil { //handle error }
A variation is to call Dialer.DialContext and apply a deadline or timeout to the context.
*net.TCPConn
If you specifically need this type instead of the following, type assert net.Conn
:
tcpConn, ok := conn.(*net.TCPConn)
2> jeanluc..:
One can use net.DialTimeout
:
func DialTimeout(network, address string, timeout time.Duration) (Conn, error) DialTimeout acts like Dial but takes a timeout. The timeout includes name resolution, if required. When using TCP, and the host in the address parameter resolves to multiple IP addresses, the timeout is spread over each consecutive dial, such that each is given an appropriate fraction of the time to connect. See func Dial for a description of the network and address parameters.