Golang Http 服务器禁用 Keep-Alive

Golang Http Server Disable Keep-Alive

我试图在 golang 中禁用 Keep-Alive Connection,但没有关于如何做的明确解释..

package main

import (
    "net/http"
    "github.com/julienschmidt/httprouter"
    "fmt"
)

func helloworld(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Hello, World!")
}


func main() {

    router := httprouter.New()

    router.GET("/", helloworld)


    fmt.Println("Running :-)")

    http.Server.SetKeepAlivesEnabled(false)
    log.Fatal(http.ListenAndServe(":3030", router))
}

谁能解决这个问题?

SetKeepAlivesEnabled 是实例配置,不是全局配置。
如果您真的需要实现这一点,请实例化您自己的服务器:

server := &http.Server{Addr: ":3030", Handler: router}

server.SetKeepAlivesEnabled(false)

server.ListenAndServe()