channel in Go

A channel is a communication pipe between goroutines which is used to pass data. A channel has a type. This is the type of data that we’ll be passing through our channel. 
```
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    c := make(chan int)

    for i := 0; i < 5; i++ {
        worker := &Worker{ id: i}
        go worker.process(c)
    }

    time.Sleep(time.Millisecond * 50)
    for i := 0; i < 10; i++ {
        c <- rand.Int()
    }
}

type Worker struct{ id int }

func (w *Worker) process(c chan int) {
    for {
        data := <-c
        fmt.Printf("worker %d got %d\n", w.id, data)
    }
}
```
Output:
```
worker 1 got 8674665223082153551
worker 1 got 6334824724549167320
worker 4 got 3916589616287113937
worker 4 got 1443635317331776148
worker 2 got 6129484611666145821
worker 1 got 605394647632969758
```
One thing to understand here is that when reading from the channel, we block till we get a message. The sender and receiver, both have to be ready. That is, we can’t send till there is a corresponding receive for that channel. 
When we send data into the channel using a goroutine, it will be blocked until the data is consumed by another goroutine. When we receive data from channel using a goroutine, it will be blocked until the data is available in the channel.
One might wonder how channels can be made unblocking. This is accomplished using buffered channels. Buffered channels are channels which are initialized with a buffer:
```
chan := make(chan int, 10)
```
Here, we create a new channel and define a capacity for the channel, which means it can only accommodate 10 values. Now, we can send values into the channel without blocking till the buffer is full and read from the channel without blocking till the buffer is empty.