我正在尝试学习围棋,并且正在操场上进行试验。我有一个非常简单的代码。我试图在 go 例程中同时使用 Structs 和 Slices。我不确定这是否是我会在生产中使用的东西,但它似乎有点不对劲,所以在这里:
func main() {
routinemsg := make(chan []Person)
routinemsg2 := make(chan []Person)
// create the person records
p1 := newPerson("john doe", 25)
p2 := newPerson("dohn joe", 52)
p3 := newPerson("bohn joo", 30)
// send a slice of Person to the first routine
go func() { routinemsg <- []Person{p1, p2} }()
// retrieve the slice from the first routine[in the append]
// append p3 to the slice retrieved from the first routine
// send the new slice to the second routine
go func() { routinemsg2 <- append(<-routinemsg, p3) }()
// I am able to see the first Println but when I insert the second one I get a deadlock error
// also, same error if I use one Println with 2 arguments.
fmt.Println(<-routinemsg)
fmt.Println(<-routinemsg2)
}
我听说过等待小组,但还不了解他们!所以,对我好点:D,谢谢你抽出时间
LEATH
慕斯王
相关分类