ln, err := net.Listen("tcp", ":8080") if err != nil { log.Fatal(err) } for { conn, err := ln.Accept() if err != nil { continue } conn.SetKeepAlive(true) conn.SetKeepAlivePeriod(time.Duration(time.Second * 10)) go handleConnection(conn) }
conn, err := net.Dial("tcp", "localhost:8080") if err != nil { log.Fatal(err) } conn.SetKeepAlive(false)This example connects to a server listening on port `8080` and disables the keep-alive functionality for the established TCP connection. Package library: The `SetKeepAlive` method is defined in Go's `net` package, which is a part of the standard library.