2. Channels

Channels are a typed, thread-safearrow-up-right queue. Channels allow different goroutines to communicate with each other.

Create a Channel

Like maps and slices, channels must be created before use. They also use the same make keyword:

ch := make(chan int)

Examples:

ch := make(chan int)           // Channel of integers
msgCh := make(chan string)     // Channel of strings
userCh := make(chan user)      // Channel of user structs

Send Data to a Channel

ch <- 69

The <- operator is called the channel operator. Data flows in the direction of the arrow. This operation will blockarrow-up-right until another goroutine is ready to receive the value.

Receive Data from a Channel

v := <-ch

This reads and removes a value from the channel and saves it into the variable v. This operation will block until there is a value in the channel to be read.

Reference Type

Like maps and slices, channels are reference types, meaning they are passed by reference by default.

No need for pointers.

Blocking and Deadlocks

A deadlockarrow-up-right is when a group of goroutines are all blocking so none of them can continue. This is a common bug that you need to watch out for in concurrent programming.

What channels are, conceptually:

  • One goroutine puts data in one end

  • Another goroutine takes data out the other end

Channel Operator: <-

The arrow shows the direction of data flow.

Send to channel:

  • Data flows into the channel

  • Reads as: "send 69 into ch"

Receive from channel:

  • Data flows out of the channel

  • Reads as: "receive from ch into v"

Complete Example

Blocking Behavior

Channels block until the other side is ready.

Send blocks:

Receive blocks:

Example: Communication Between Goroutines

Flow:

1

Create channel

2

Start goroutine that will send

3

Main waits to receive

4

Goroutine sends, main receives

5

Both continue

Multiple Sends and Receives

Deadlock Example

This will deadlock:

Why:

  • ch <- 42 blocks waiting for a receiver

  • But there's no goroutine to receive

  • Program freezes forever

Error:

Fixing the Deadlock

Solution: use a goroutine

Now it works because:

  • Goroutine sends

  • Main receives

  • They coordinate through the channel

Real-World Example: Sending Results

Channel Direction Arrows

Visual guide:

Common Patterns

Pattern: Send and close

Pattern: Receive until closed

Pattern: Multiple workers

Key Takeaways

1

Create: ch := make(chan Type)

2

Send: ch <- value (blocks until received)

3

Receive: value := <-ch (blocks until sent)

4

Reference type: Pass by reference automatically

5

Watch for deadlocks: Always have sender and receiver

Quick Reference

Operation
Syntax
Blocks Until

Create

ch := make(chan int)

N/A

Send

ch <- 42

Someone receives

Receive

v := <-ch

Someone sends

Close

close(ch)

N/A

Remember: The arrow <- shows data flow direction!

Assignment

Run the program. You'll see that it deadlocks and never exits. The sendIsOld function is trying to send on a channel, but no other goroutines are running that can accept the value from the channel.

Fix the deadlock by spawning a goroutine to send the "is old" values.

Original code:

Solution (spawn the sender as a goroutine):