例子
package main import ( "fmt" "time" ) var channel = make(chan int, 5) func main() { go producer() // 遍历通道 for i:= range channel { fmt.Printf("consume %d\n", i) } time.Sleep(time.Duration(5) * time.Second) } func producer() { for i := 0; i <= 10; i++ { channel <- i fmt.Printf("produce %d\n", i) } // 关闭通道 close(channel) }