我应该将 goroutines 与 http.ListenAndServe 一起使用吗?

Should I be using goroutines with http.ListenAndServe?

如果我使用 http.ListenAndServe 在用户点击 URL 时提供响应,我是否应该将函数中的相应操作作为 goroutine 触发?

例如,假设我正在 /:

收听
func main() {
    http.HandleFunc("/", provideMainContent)
}

func provideMainContent(w http.ResponseWriter, r *http.Request) {
    /// Bunch of code, looks up details in databases, parses, then returns
}

provideMainContent 中的一堆代码是否应该包装在一个 goroutine 中,这样它就不会减慢事后发生的任何潜在请求?

简答,否

来自 http.Serve 的 GoDoc:

Serve accepts incoming HTTP connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them.

然而,正如 @Mellow Marmot 链接的问题中提到的,在某些情况下,您可能希望在处理程序 return 中生成一个 goroutine 来进行一些处理,以便请求者执行不必等待所有处理完成才能获得响应。