GOLANG

Anatomy of Channels in Go - Concurrency in Go

Uday Hiwarale
RunGo
27 min readNov 19, 2018

--

What are the channels?

A channel is a communication object using which goroutines can communicate with each other. Technically, a channel is a data transfer pipe where data can be passed into or read from. Hence one goroutine can send data into a channel, while other goroutines can read that data from the same channel.

Declaring a channel

Go provides chan keyword to create a channel. A channel can transport data of only one data type. No other data types are allowed to be transported from that channel.

https://play.golang.org/p/iWOFLfcgfF-

Above program declares a channel c which can transport data type of int. Above program prints <nil> because zero-value of a channel is nil. But a nil channel is not useful. You can not pass data to or read data from a channel which is nil. Hence, we have to use make function to create a ready-to-use channel.

https://play.golang.org/p/N4dU7Ql9bK7

--

--