go channel read/write and direction

tags: learning go programming

content

  • a channel in go could be read-only or write-only
  • a read-only or write-only channel is achieved with type casting
    • read-only: <-chan (value coming out from the channel)
    • write-only: chan<- (value going into the channel)
func readChannel(ch <-chan int) {...}
func writeChannel(ch chan<- int) {...}
  • this read-only / write-only can also be seen as channel’s direction

up

down