为什么这个 goroutine 会阻塞?

Sun*_*nny 2 go goroutine

这个 goroutine 阻塞...

go log.Fatal(http.ListenAndServe(":8000", nil))
log.Print("This doesn't print")
Run Code Online (Sandbox Code Playgroud)

这个 goroutine 不会阻塞...

go func() {
    log.Fatal(http.ListenAndServe(":8000", nil))
}()
log.Print("This prints")
Run Code Online (Sandbox Code Playgroud)

这个 goroutine 也不会阻塞...

go http.ListenAndServe(":8000", nil)
log.Print("This prints")
Run Code Online (Sandbox Code Playgroud)

Dan*_*ilo 6

这是根据规范:

函数值和参数在调用 goroutine 中照常计算

https://golang.org/ref/spec#Go_statements

go log.Fatal(http.ListenAndServe(":8000", nil))
Run Code Online (Sandbox Code Playgroud)

第一个参数是

http.ListenAndServe(":8000", nil)
Run Code Online (Sandbox Code Playgroud)

在将函数log.Fatal作为 goroutine 执行之前将对其进行评估,从而阻塞。