All About Channels in Golang
go channels and its working#
Goroutines are main selling point for goland because of it is lightweight and very easy to create, but what about communication between channels.
As we know channels run in seperate thread we can not determine the running state of goroutines. so there is channels commes into the picture.
Channels are always pass by reference only. that’s why we can send from one end and receive from another end without creating pointer or anything
There are two types of channels
- Buffered Channels
- Unbuffered Channels
Buffered Channel#
Buffered channels are like we are giving some storage space to that channels and we can create that like below.
bufferedCh := make(chan int, 3)
we have created a buffered channel of storage 3 means it can store 3 ints without any receiver. we can push upto three ints and that will be stored into that like below.
for i:=0; i<3; i++ {
bufferedCh <- i
}
for sending data to the channel we use chan_name<-
and for receiving data from the channel we use <-chan_name
Unbuffered Channel#
unbuffered channels do not have any storage so whenever we are sending data to that channel there must be receiver to receive that data otherwise we will get deadlock error.
unbuff := make(chan int)
receiving from channels are blocking in nature means if you are receiving from channel like below that will block the execution until it receives any data
data := <-chan_name
// any code below will be blocked until above channel receives data
thats why you cannot send and receive from unbuffered channel in same goroutine you must have to use seperate gorutine for either sending or receiving
like below example
package main
import "time"
func main() {
ch := make(chan int)
go func(){
ch <- 25
}()
fmt.Println(<-ch)
time.Sleep(time.Second)
}
we are sending data from different goroutine and receiving in main goroutine.
receiving from multiple channels#
Suppose there are multiple channels in your programme and you want to receive data from either of them then there is special way built into the language
we can use select case for channels as well but they are different from original one
select {
case d:= <-chan1:
fmt.Println(d)
case b:= <-chan2:
fmt.Println(b)
.
.
.
and so on
}
above code will be blocking and receiving from both channel one at time.
look at the example below for receiving from multiple channels
package main
import "time"
func main() {
chan1 := make(chan int)
chan2 := make(chan int)
go func(){
select {
case d:= <-chan1:
fmt.Println(d)
case b:= <-chan2:
fmt.Println(b)
}
}()
chan1 <- 5
chan2 <- 10
time.Sleep(time.Second)
}