go routine chan用法

点击查看代码
package main

import (
	"fmt"
	"time"
)

func write(ch chan int) {
	for i := 0; i < 100; i++ {
		ch <- i
		fmt.Println("Put data:", i)
	}
}

func read(ch chan int) {
	for {
		fmt.Println("Read data:",<- ch)
	}
}

func main() {
	intChan := make(chan int, 10)
	go write(intChan)
	go read(intChan)
	time.Sleep(10 * time.Second)
}
posted @ 2022-03-05 12:21  ty1539  阅读(9)  评论(0编辑  收藏  举报