```go
package main
import "net/http"
func main() {
c := make(chan string)
http.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
msg,_ := <- c
w.Write([]byte(msg))
})
http.HandleFunc("/post", func(w http.ResponseWriter, r *http.Request) {
c <- "world"
w.Write([]byte(" world!"))
})
http.ListenAndServe("localhost:3000", nil)
}
```
期望 curl http://localhost:3000/post 写channel
curl http://localhost:3000/get 读channel
但是一运行就死锁了
https://juejin.im/post/5afce9e1518825426a1fe3a1
https://studygolang.com/articles/9943
https://github.com/golang/go/blob/master/src/net/http/server.go#L2900-L2932
为每一个请求启动一个协程
c := make(chan string)是全局变量,在哪个协程里面都可以访问
#6
更多评论
楼主好像对channel有一些误解.
你写的这个东西是无法处理通道的读写通讯的.
通道是等在那里的, 一旦写入就要有人读出, 否则就堵塞...
明白了么...
#1