conn, err := net.Dial("tcp", "localhost:8080") if err != nil { // handle error } tcpConn := conn.(*net.TCPConn) tcpConn.SetKeepAlivePeriod(time.Second * 60)
listener, err := net.Listen("tcp", ":8080") if err != nil { // handle error } tcpListener := listener.(*net.TCPListener) tcpListener.SetKeepAlivePeriod(0)In this example, we are creating a TCP listener on port 8080 using the net.Listen function. We get the underlying TCPListener by type asserting the listener to *net.TCPListener. Finally, we set the keepalive period to the default value using the SetKeepAlivePeriod method. Package library: net