17-golang中单向channel的应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33781658/article/details/83788086

将channel传入方法

但是可以转换为单向的channel

channelSend chan<- int

channelReceive <-chan int

func main() {

   channel := make(chan int)

   go producer(channel)

   consumer(channel)
}

func producer(send chan<- int) {
   for i := 0; i < 10; i++ {
      send <- i
   }
   close(send)
}

func consumer(receive <-chan int) {
   for num := range receive {
      fmt.Println("receive", num)
   }
}

猜你喜欢

转载自blog.csdn.net/qq_33781658/article/details/83788086