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 但是一运行就死锁了
有疑问加站长微信联系(非本文作者)

楼主好像对channel有一些误解. 你写的这个东西是无法处理通道的读写通讯的. 通道是等在那里的, 一旦写入就要有人读出, 否则就堵塞... 明白了么...
lz是什么环境,我这里测试正常,只访问其中一个route会阻塞,再访问另一个的话就ok了, channel可以写通
程序是正常的,我只是想确认一下golang web框架处理http请求是不是来一个请求,就用go func(){ ... }() 来处理的
可以去看http包源码, https://github.com/golang/go/blob/master/src/net/http/server.go#L2900-L2932
是一个gorutine,可以看源码
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)是全局变量,在哪个协程里面都可以访问