import ( "net" "time" ) func main() { conn, _ := net.Dial("tcp", "example.com:80") conn.SetReadDeadline(time.Now().Add(5*time.Second)) // Perform read operation on TCPConn }
import ( "net" "time" ) func main() { ln, _ := net.Listen("tcp", ":9000") conn, _ := ln.Accept() conn.SetReadDeadline(time.Now().Add(10*time.Second)) // Perform read operation on TCPConn }In this example, the SetReadDeadline method is used to set a timeout of 10 seconds for a read operation on a TCPConn that is accepted through a listener on port 9000. When the read operation is performed, it will wait for new data for up to 10 seconds before aborting and returning an error if no data is received. Package library: net