标签:send make fun ann class 16px read_only color read
Go中channel可以是只读、只写、同时可读写的。
//定义只读的channel
read_only := make (<-chan int)
//定义只写的channel
write_only := make (chan<- int)
//可同时读写
read_write := make (chan int)
定义只读和只写的channel意义不大,一般用于在参数传递中,见代码:
package main import ( "fmt" "time" ) func main() { c := make(chan int) go send(c) go recv(c) time.Sleep(3 * time.Second) } //只能向chan里写数据 func send(c chan<- int) { for i := 0; i < 10; i++ { c <- i } } //只能取channel中的数据 func recv(c <-chan int) { for i := range c { fmt.Println(i) } }
标签:send make fun ann class 16px read_only color read
原文地址:https://www.cnblogs.com/lmh001/p/10298374.html