这个goroutine阻止......
go log.Fatal(http.ListenAndServe(":8000", nil))
log.Print("This doesn't print")
这个goroutine并没有阻止......
go func() {
log.Fatal(http.ListenAndServe(":8000", nil))
}()
log.Print("This prints")
这个goroutine也没有阻止......
go http.ListenAndServe(":8000", nil)
log.Print("This prints")
答案 0 :(得分:2)
这是根据规范:
在调用goroutine
中照常评估函数值和参数
https://golang.org/ref/spec#Go_statements
在
go log.Fatal(http.ListenAndServe(":8000", nil))
第一个参数是
http.ListenAndServe(":8000", nil)
将在执行函数log.Fatal
作为goroutine之前进行评估,从而阻塞。
答案 1 :(得分:-2)
嗯, 我跑了程序:
package main
import (
"net/http"
"log"
)
func main() {
go log.Fatal(http.ListenAndServe(":8000", nil))
log.Print("This doesn't print")
}
它似乎运作良好:
curl 127.0.0.1:8000 -v
* Rebuilt URL to: 127.0.0.1:8000/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Fri, 24 Feb 2017 08:22:19 GMT
< Content-Length: 19
<
404 page not found
* Curl_http_done: called premature == 0
* Connection #0 to host 127.0.0.1 left intact
我的版本:
go1.7.3 darwin/amd64
请指定有关运行时的更多信息,例如go version,architecture等。