标签:port sleep bool package read imp func pac second
package main import ( "fmt" "time" ) func readChan(inchan chan int, exitchan chan bool) { for { time.Sleep(time.Millisecond * 500) res, ok := <-inchan if !ok { break } fmt.Printf("读取%d\n", res) } exitchan <- true close(exitchan) } func writeChan(inchan chan int) { for i := 1; i <= 50; i++ { inchan <- i fmt.Printf("写入%d\n", i) } close(inchan) } func main() { inchan := make(chan int, 10) exitchan := make(chan bool, 1) go writeChan(inchan) go readChan(inchan, exitchan) for { _, ok := <-exitchan if !ok { break } } }
标签:port sleep bool package read imp func pac second
原文地址:https://www.cnblogs.com/LC161616/p/10080596.html