package main //这个例子本质是同步模式,一个goroutine负责写,一个goroutine负责读。 import ( "fmt" "time" ) func main() { done := make(chan struct{}) c := make(chan int) go func() { for i := 0; i < 5; i++ { c <- i fmt.Println("send", i) time.Sleep(1000 * time.Millisecond) } close(done) }() go func() { for j := range c { fmt.Println("receive", j) } }() //go关键字创建并发任务单元。新任务被放到系统队列。但不会立刻执行。 //因此,需要下面这行语句,等待channle关闭,这样前面创建的两个任务才会进入执行。 //否则程序一闪而过,不会看到输出。 <-done }
[blockchain-025] go语言的channel极简例子
猜你喜欢
转载自blog.csdn.net/u011539200/article/details/80004365
今日推荐
周排行