0
0
Goprogramming~20 mins

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

Choose your learning style9 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.