这个 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)
这是根据规范:
函数值和参数在调用 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 执行之前将对其进行评估,从而阻塞。
| 归档时间: |
|
| 查看次数: |
1511 次 |
| 最近记录: |