Bird
Raised Fist0
Goprogramming~10 mins

Channel creation in Go - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Channel creation
Start
↓
Declare channel variable
↓
Create channel with make()
↓
Channel ready for use
↓
Use channel to send/receive
↓
End
This flow shows how a channel variable is declared, created with make(), and then ready to send or receive data.
Execution Sample
Go
package main
import "fmt"
func main() {
  ch := make(chan int)
  fmt.Println(ch)
}
This code creates an integer channel and prints its value (memory address).
Execution Table
StepActionCode LineChannel Variable ValueNotes
1Start programfunc main() {nil (not declared yet)Program begins
2Create channel with makech := make(chan int)0xc0000a4000 (example address)Channel created and assigned
3Print channel valuefmt.Println(ch)0xc0000a4000Prints channel memory address
4Program ends}0xc0000a4000Program ends
💡 Program ends after printing the channel variable value.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
chnil0xc0000a40000xc0000a40000xc0000a4000
Key Moments - 2 Insights
Why does the channel variable 'ch' have a memory address after make()?
Because make(chan int) creates a new channel object in memory and returns its address, which is stored in 'ch' as shown in step 2 of the execution table.
Is the channel usable immediately after creation?
Yes, after make() returns the channel, it is ready to send and receive data, as indicated by the 'Channel ready for use' step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'ch' after step 2?
Anil
B0xc0000a4000 (example address)
C0
Dundefined
💡 Hint
Check the 'Channel Variable Value' column at step 2 in the execution table.
At which step is the channel created and assigned to 'ch'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the action 'Create channel with make' in the execution table.
If we remove make() and just declare 'var ch chan int', what would be the initial value of 'ch'?
Anil
B0
CA valid channel address
DAn error
💡 Hint
Refer to Go language rules and the 'Start' value of 'ch' in variable_tracker.
Concept Snapshot
Channel creation in Go:
- Declare channel variable: var ch chan Type
- Create channel with make: ch := make(chan Type)
- Channel holds a reference (memory address)
- Ready to send/receive after make
- Printing channel shows its address
Full Transcript
This visual execution trace shows how a channel is created in Go. First, the program starts with no channel variable. Then, using make(chan int), a new channel is created and assigned to variable 'ch'. This variable now holds a memory address pointing to the channel. Printing 'ch' outputs this address. The channel is ready to use after creation. The variable tracker confirms 'ch' changes from nil to a valid address after make(). Key points include understanding that make() allocates the channel and returns its reference, and that declaring a channel without make() leaves it nil and unusable. The quizzes test understanding of channel value changes and creation steps.

Practice

(1/5)
1. What is the correct way to create an unbuffered channel of integers in Go?
easy
A. ch := chan int{}
B. ch := new(chan int)
C. ch := make(chan int)
D. ch := make(int chan)

Solution

  1. Step 1: Understand channel creation syntax

    Channels are created using the built-in make function with the syntax make(chan Type).
  2. Step 2: Identify the correct option

    ch := make(chan int) uses make(chan int), which is the correct syntax for an unbuffered integer channel.
  3. Final Answer:

    ch := make(chan int) -> Option C
  4. Quick Check:

    make(chan int) = correct channel creation [OK]
Hint: Use make(chan Type) to create channels in Go [OK]
Common Mistakes:
  • Using new(chan int) instead of make
  • Incorrect syntax like chan int{}
  • Swapping order in make(int chan)
2. Which of the following creates a buffered channel of strings with capacity 5?
easy
A. ch := make(chan string, cap=5)
B. ch := make(chan string, 5)
C. ch := make(chan string, "5")
D. ch := make(chan string)

Solution

  1. Step 1: Recall buffered channel syntax

    Buffered channels are created by passing a second argument to make, which is the capacity (an integer).
  2. Step 2: Identify the correct option

    ch := make(chan string, 5) correctly uses make(chan string, 5) to create a buffered string channel with capacity 5.
  3. Final Answer:

    ch := make(chan string, 5) -> Option B
  4. Quick Check:

    make(chan string, 5) = buffered channel [OK]
Hint: Add capacity as second arg in make for buffered channels [OK]
Common Mistakes:
  • Omitting capacity for buffered channel
  • Using string instead of int for capacity
  • Incorrect syntax with named parameters
3. What will be the output of this Go code?
ch := make(chan int, 2)
ch <- 10
ch <- 20
fmt.Println(<-ch)
fmt.Println(<-ch)
medium
A. 10 20
B. 20 10
C. 10 10
D. Compilation error

Solution

  1. Step 1: Understand buffered channel behavior

    The channel has capacity 2, so it can hold two values without blocking. Values 10 and 20 are sent in order.
  2. Step 2: Read values from channel in FIFO order

    Receiving from the channel returns values in the order sent: first 10, then 20.
  3. Final Answer:

    10 20 -> Option A
  4. Quick Check:

    Buffered channel FIFO order = 10 then 20 [OK]
Hint: Buffered channels output values in send order (FIFO) [OK]
Common Mistakes:
  • Assuming output reverses order
  • Expecting runtime or compile error
  • Confusing buffered with unbuffered channel behavior
4. Identify the error in this Go code snippet:
ch := make(chan int)
ch <- 5
fmt.Println(<-ch)
medium
A. Deadlock: sending on unbuffered channel without receiver
B. Syntax error in channel creation
C. Cannot print channel value directly
D. No error, code runs fine

Solution

  1. Step 1: Analyze unbuffered channel send

    Unbuffered channels block the sender until a receiver is ready. Here, send happens before receive.
  2. Step 2: Identify deadlock situation

    Since no goroutine is receiving concurrently, the send blocks forever causing deadlock.
  3. Final Answer:

    Deadlock: sending on unbuffered channel without receiver -> Option A
  4. Quick Check:

    Unbuffered send blocks without receiver = deadlock [OK]
Hint: Unbuffered send blocks until receive happens [OK]
Common Mistakes:
  • Thinking code compiles but runs fine
  • Confusing syntax error with runtime deadlock
  • Assuming print causes error
5. You want to create a channel that can hold up to 3 float64 values without blocking the sender immediately. Which code snippet correctly does this and sends three values safely?
hard
A. ch := make(chan float64, 3) <-ch <-ch <-ch
B. ch := make(chan float64) ch <- 1.1 ch <- 2.2 ch <- 3.3
C. ch := make(chan float64, 2) ch <- 1.1 ch <- 2.2 ch <- 3.3
D. ch := make(chan float64, 3) ch <- 1.1 ch <- 2.2 ch <- 3.3

Solution

  1. Step 1: Create buffered channel with capacity 3

    Use make(chan float64, 3) to create a channel that can hold 3 float64 values without blocking.
  2. Step 2: Send three values safely

    Sending three values to this buffered channel will not block because capacity matches the number of sends.
  3. Final Answer:

    ch := make(chan float64, 3) ch <- 1.1 ch <- 2.2 ch <- 3.3 -> Option D
  4. Quick Check:

    Buffered channel capacity 3 allows 3 sends without blocking [OK]
Hint: Buffer size must match or exceed sends to avoid blocking [OK]
Common Mistakes:
  • Using unbuffered channel for multiple sends
  • Buffer smaller than sends causing deadlock
  • Receiving before sending without goroutine
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard