为什么这个goroutine会阻塞?
这个goroutine块为什么这个goroutine会阻塞?,go,goroutine,Go,Goroutine,这个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") go http.ListenAndServe(":8000", nil) log.Print("T
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")
go http.ListenAndServe(":8000", nil)
log.Print("This prints")
这个goroutine也不会阻止
go func() {
log.Fatal(http.ListenAndServe(":8000", nil))
}()
log.Print("This prints")
go http.ListenAndServe(":8000", nil)
log.Print("This prints")
嗯,
我运行了一个程序:
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
请指定有关运行时的更多信息,如go版本、体系结构等。这取决于规范: 函数值和参数在调用goroutine中按常规进行计算 在 第一个参数是
http.ListenAndServe(":8000", nil)
它将在作为goroutine执行函数log.Fatal之前进行评估,从而阻塞。go log.Fatal(http.ListenAndServe(“:8000”,nil))
相当于
e := http.ListenAndServe(":8000", nil)
go log.Fatal(e)
当然它会阻塞。至于go func(){
log.Fatal(http.ListenAndServe(“:8000”,无))
}()
它作为一个独立的goroutine启动函数的执行。然后调用log.Print(“This prints”)
,因为记录器可以从多个goroutine同时使用,所以它会打印