Go:动态的创建多个chan

让chan作为map的value。

The make(chan int, 2) part makes the channel buffered with a buffer length of 2. 

package main

import "fmt"

func main() {
	things := make(map[string](chan int))
	
	things["stuff"] = make(chan int, 2)
	things["stuff"] <- 2
	mything := <-things["stuff"]
	fmt.Printf("my thing: %d", mything)
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页