3 回答
TA贡献1795条经验 获得超7个赞
通道的make(chan int)
隐式大小为零(
大小为零的通道是无缓冲的。指定大小的通道 make(chan int, n) 被缓冲。
在这里,通道<-ok
或ok <-
将被阻塞,直到有人处理它(并发)。所以,ok := make(chan int)
改为ok := make(chan int,1)
package main
import "fmt"
func main() {
ok := make(chan int, 1)
ok <- 1
x := <- ok
fmt.Println(x)
}
或者同时处理它
package main
import "fmt"
func main() {
ok := make(chan int)
go func(){
ok <- 1
}()
x := <- ok
fmt.Println(x)
}
TA贡献2011条经验 获得超2个赞
TA贡献1846条经验 获得超7个赞
除了使用调试器,我没有想到任何其他方法。尝试钻研
> main.main() ./main.go:20 (PC: 0x4b68dc)
15: func main() {
16:
17: c := make(chan int,10)
18: a:=1
19: c<-a
=> 20: fmt.Println(<-c)
21:
22:
23: }
(dlv) p c
chan int {
qcount: 1,
dataqsiz: 10,
buf: *[10]int [1,0,0,0,0,0,0,0,0,0],
elemsize: 8,
closed: 0,
elemtype: *runtime._type {size: 8, ptrdata: 0, hash: 4149441018, tflag: tflagUncommon|tflagExtraStar|tflagNamed|tflagRegularMemory (15), align: 8, fieldAlign: 8, kind: 2, equal: runtime.memequal64, gcdata: *0, str: 847, ptrToThis: 26368},
sendx: 1,
recvx: 0,
recvq: waitq<int> {
first: *sudog<int> nil,
last: *sudog<int> nil,},
sendq: waitq<int> {
first: *sudog<int> nil,
last: *sudog<int> nil,},
lock: runtime.mutex {
lockRankStruct: runtime.lockRankStruct {},
key: 0,},}
(dlv)
- 3 回答
- 0 关注
- 143 浏览
添加回答
举报