Standard library / net/http

http.Server.SetKeepAlivesEnabled()

Enables or disables HTTP keep-alives for the server. Disabling is generally reserved for shutdown or constrained environments. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (srv *Server) SetKeepAlivesEnabled(v bool)

Parameters & Attributes

NameTypeDescription
vboolWhether to enable connection keep-alives.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "net/http/httptest"  "time")
func main() {  s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "ok") }))  s.Config.SetKeepAlivesEnabled(false)  s.Start()  defer s.Close()  c := &http.Client{Timeout: time.Second}  r := must(c.Get(s.URL))  defer r.Body.Close()  fmt.Println(r.Close)}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference