dongrong9938 2017-02-24 07:19
浏览 14
已采纳

为什么此goroutine会阻止?

This goroutine blocks...

go log.Fatal(http.ListenAndServe(":8000", nil))
log.Print("This doesn't print")

This goroutine doesn't block...

go func() {
    log.Fatal(http.ListenAndServe(":8000", nil))
}()
log.Print("This prints")

This goroutine also doesn't block...

go http.ListenAndServe(":8000", nil)
log.Print("This prints")
  • 写回答

3条回答 默认 最新

  • doudun3910 2017-02-24 08:28
    关注

    This is according to the spec:

    The function value and parameters are evaluated as usual in the calling goroutine

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

    In

    go log.Fatal(http.ListenAndServe(":8000", nil))
    

    The first parameter is

    http.ListenAndServe(":8000", nil)
    

    which will be evaluated before executing the function log.Fatal as a goroutine, thus blocking.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?