Bird
Raised Fist0
Goprogramming~20 mins

Sending and receiving values in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Channel Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Go code using channels?
Consider the following Go program. What will it print when run?
Go
package main
import "fmt"
func main() {
    ch := make(chan int)
    go func() {
        ch <- 42
    }()
    val := <-ch
    fmt.Println(val)
}
ADeadlock error
B0
CCompilation error
D42
Attempts:
2 left
💡 Hint
Think about how the goroutine sends a value and the main goroutine receives it.
❓ Predict Output
intermediate
2:00remaining
What happens if you try to receive from a closed channel?
What will this Go program print?
Go
package main
import "fmt"
func main() {
    ch := make(chan int, 1)
    ch <- 10
    close(ch)
    val1, ok1 := <-ch
    val2, ok2 := <-ch
    fmt.Println(val1, ok1)
    fmt.Println(val2, ok2)
}
A
10 true
0 false
B
10 true
10 true
C
0 false
0 false
DCompilation error
Attempts:
2 left
💡 Hint
Remember what happens when you receive from a closed channel with buffered values.
🔧 Debug
advanced
2:00remaining
Why does this Go program deadlock?
Examine the code below. Why does it cause a deadlock?
Go
package main
func main() {
    ch := make(chan int)
    ch <- 5
    val := <-ch
    println(val)
}
ABecause the channel is buffered and full
BBecause the send operation blocks forever since no goroutine is receiving
CBecause the channel is closed before sending
DBecause the receive operation blocks forever since no value is sent
Attempts:
2 left
💡 Hint
Think about how unbuffered channels block on send and receive.
🧠 Conceptual
advanced
1:30remaining
What is the effect of closing a channel in Go?
Which statement about closing a channel is true?
AClosing a channel allows receivers to detect no more values will be sent
BClosing a channel prevents any further values from being received
CClosing a channel automatically empties all buffered values
DClosing a channel causes a runtime panic if any goroutine is receiving
Attempts:
2 left
💡 Hint
Think about how receivers can check if a channel is closed.
❓ Predict Output
expert
3:00remaining
What is the output of this Go program with multiple goroutines and channels?
Analyze the code and select the correct output.
Go
package main
import (
    "fmt"
    "sync"
)
func main() {
    ch := make(chan int)
    var wg sync.WaitGroup
    wg.Add(2)
    go func() {
        defer wg.Done()
        for i := 0; i < 3; i++ {
            ch <- i
        }
    }()
    go func() {
        defer wg.Done()
        for i := 3; i < 6; i++ {
            ch <- i
        }
    }()
    go func() {
        wg.Wait()
        close(ch)
    }()
    sum := 0
    for v := range ch {
        sum += v
    }
    fmt.Println(sum)
}
ACompilation error
B21
C15
DDeadlock error
Attempts:
2 left
💡 Hint
Consider the values sent on the channel and how the sum is calculated.

Practice

(1/5)
1. What is the correct way to send a value 5 to a channel named ch in Go?
easy
A. ch <- 5
B. 5 <- ch
C. send(ch, 5)
D. ch = 5

Solution

  1. Step 1: Understand channel send syntax

    In Go, to send a value to a channel, you write channel <- value.
  2. Step 2: Apply syntax to given variables

    Here, the channel is ch and the value is 5, so the correct send is ch <- 5.
  3. Final Answer:

    ch <- 5 -> Option A
  4. Quick Check:

    Send syntax = channel <- value [OK]
Hint: Remember: channel <- value sends data [OK]
Common Mistakes:
  • Reversing send syntax as value <- channel
  • Using assignment operator instead of send
  • Calling a non-existent send function
2. Which of the following is the correct syntax to receive a value from a channel ch and store it in variable v?
easy
A. v := ch <-
B. v = ch <-
C. v = ch.receive()
D. v := <- ch

Solution

  1. Step 1: Recall channel receive syntax

    To receive a value from a channel, Go uses value := <- channel.
  2. Step 2: Match syntax with variables

    Here, v receives from ch, so v := <- ch is correct.
  3. Final Answer:

    v := <- ch -> Option D
  4. Quick Check:

    Receive syntax = v := <- ch [OK]
Hint: Use := <- channel to receive value [OK]
Common Mistakes:
  • Placing <- after channel name
  • Using method calls like receive()
  • Using assignment = instead of := for declaration
3. What will be the output of the following Go program?
package main
import "fmt"
func main() {
    ch := make(chan int)
    go func() {
        ch <- 10
    }()
    val := <-ch
    fmt.Println(val)
}
medium
A. 0
B. Compilation error
C. 10
D. Deadlock error

Solution

  1. Step 1: Understand channel creation and goroutine

    The channel ch is created and a goroutine sends 10 to it.
  2. Step 2: Receive value and print

    The main goroutine receives the value from ch and prints it.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Send and receive work correctly, output = 10 [OK]
Hint: Send in goroutine, receive in main prints sent value [OK]
Common Mistakes:
  • Expecting zero value instead of sent value
  • Thinking channel blocks forever causing deadlock
  • Confusing send and receive syntax
4. Identify the error in this Go code snippet:
package main
func main() {
    ch := make(chan int)
    ch <- 5
    val := <- ch
    println(val)
}
medium
A. Channel is not created properly
B. Sending to channel blocks forever causing deadlock
C. Receiving from channel is incorrect syntax
D. Variable val is not declared

Solution

  1. Step 1: Analyze channel usage without goroutine

    The code sends to ch but no goroutine is receiving yet, so send blocks forever.
  2. Step 2: Understand deadlock cause

    Since the send blocks and main goroutine waits, program deadlocks before receiving.
  3. Final Answer:

    Sending to channel blocks forever causing deadlock -> Option B
  4. Quick Check:

    Send blocks without concurrent receiver [OK]
Hint: Send blocks if no goroutine receives concurrently [OK]
Common Mistakes:
  • Assuming send works without concurrent receive
  • Thinking channel creation is wrong
  • Confusing syntax errors with runtime deadlock
5. You want to send multiple values 1, 2, 3 to a channel ch and receive them in order. Which code snippet correctly does this without causing deadlock?
hard
A.
ch := make(chan int)
go func() {
    ch <- 1
    ch <- 2
    ch <- 3
}()
for i := 0; i < 3; i++ {
    fmt.Println(<-ch)
}
B.
ch := make(chan int)
ch <- 1
ch <- 2
ch <- 3
for i := 0; i < 3; i++ {
    fmt.Println(<-ch)
}
C.
ch := make(chan int, 1)
ch <- 1
ch <- 2
ch <- 3
for i := 0; i < 3; i++ {
    fmt.Println(<-ch)
}
D.
ch := make(chan int)
for i := 1; i <= 3; i++ {
    ch <- i
}
for i := 0; i < 3; i++ {
    fmt.Println(<-ch)
}

Solution

  1. Step 1: Check for deadlock prevention

    ch := make(chan int)
    go func() {
        ch <- 1
        ch <- 2
        ch <- 3
    }()
    for i := 0; i < 3; i++ {
        fmt.Println(<-ch)
    }
    uses a goroutine to send values, so sending won't block the main goroutine.
  2. Step 2: Confirm receiving order and printing

    Main goroutine receives three values in order and prints them correctly.
  3. Final Answer:

    This code runs without deadlock and prints 1, 2, 3 -> Option A
  4. Quick Check:

    Use goroutine to send multiple values safely [OK]
Hint: Send in goroutine or use buffered channel to avoid deadlock [OK]
Common Mistakes:
  • Sending multiple values on unbuffered channel without goroutine
  • Assuming unbuffered channel allows multiple sends without receive
  • Forgetting to import fmt for printing
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