[英]why goroutine block main func in this http server?
我想設置一個帶有 httprouter 監聽兩個端口8888
和8080
的 http 服務器,就像下面的代碼一樣。
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func main() {
router := httprouter.New()
router.GET("/", Index)
fmt.Println("listen on 8080")
// this is where blocked
go log.Fatal(http.ListenAndServe(":8080", router))
fmt.Println("listen on 8888")
log.Fatal(http.ListenAndServe(":8888", router))
}
但它不能正常工作,我的服務器只監聽8080
。如果我做一些改變:
go func() { log.Fatal(http.ListenAndServe(":8080", router)) }()
它在8080
和8888
上都能很好地工作。為什么? 是closure
還是別的什么?
函數值和參數在調用 goroutine 中照常評估
— Go 語言規范,“Go 語句” 。
您正在創建呼叫到的goroutine log.Fatal
,但參數log.Fatal
預先評估,在主夠程。 Fatal
的參數是http.ListenAndServe
的返回值。 所以新的 goroutine 直到ListenAndServe
返回后ListenAndServe
啟動。
由於http.ListenAndServe()
是阻塞的,並且在您的場景中,有兩個,然后嘗試將其中一個放在 goroutine 中。 這個想法是將這兩個 Web 服務器初始化語句的執行分開到單獨的 goroutine 中。
func main() {
router := httprouter.New()
router.GET("/", Index)
go func() {
fmt.Println("listen on 8080")
log.Fatal(http.ListenAndServe(":8080", router))
}()
fmt.Println("listen on 8888")
log.Fatal(http.ListenAndServe(":8888", router))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.