Bird
Raised Fist0
Goprogramming~10 mins

Channel closing behavior in Go - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to close the channel.

Go
ch := make(chan int)
// Close the channel
[1](ch)
Drag options to blanks, or click blank then click option'
Aclose
BcloseChan
CchanClose
Dshutdown
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent function like closeChan or chanClose.
Trying to assign nil to the channel instead of closing it.
2fill in blank
medium

Complete the code to receive from a closed channel without blocking.

Go
ch := make(chan int)
go func() {
    close(ch)
}()

value, [1] := <-ch
Drag options to blanks, or click blank then click option'
Aclosed
Bdone
Cok
Dreceived
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name other than 'ok' which is conventional.
Not using the two-value receive syntax.
3fill in blank
hard

Fix the error in the code that tries to send on a closed channel.

Go
ch := make(chan int)
go func() {
    close(ch)
}()

// This will cause a panic
[1] ch <- 5
Drag options to blanks, or click blank then click option'
A// do not send
Bclose(ch)
Cselect
Dch <- 5
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to close the channel again.
Using select without proper case to avoid sending.
4fill in blank
hard

Fill both blanks to create a map comprehension that stores lengths of words longer than 3 characters.

Go
words := []string{"go", "code", "chat", "bot"}
lengths := map[string]int{}
for _, word := range words {
    if len(word) [1] 3 {
        lengths[[2]] = len(word)
    }
}
Drag options to blanks, or click blank then click option'
A>
Bword
C<
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than.
Using len instead of word as the map key.
5fill in blank
hard

Fill all three blanks to create a select statement that handles sending, receiving, and default case on a channel.

Go
select {
case [1] <- 10:
    fmt.Println("Sent 10")
case val := <-[2]:
    fmt.Println("Received", val)
default:
    fmt.Println([3])
}
Drag options to blanks, or click blank then click option'
Ach
C"No communication"
D"Waiting"
Attempts:
3 left
💡 Hint
Common Mistakes
Using different channels for send and receive.
Not using a string literal in the default case.

Practice

(1/5)
1. What happens when you close a channel in Go?
easy
A. The channel is deleted from memory immediately
B. Senders can still send values without error
C. Receivers know no more values will be sent
D. The channel blocks all operations permanently

Solution

  1. Step 1: Understand channel closing purpose

    Closing a channel signals receivers that no more values will be sent.
  2. Step 2: Effects on senders and receivers

    Senders cannot send after closing; receivers get zero value and ok=false.
  3. Final Answer:

    Receivers know no more values will be sent -> Option C
  4. Quick Check:

    close(channel) signals receivers [OK]
Hint: Closing signals receivers no more data comes [OK]
Common Mistakes:
  • Thinking senders can send after close
  • Believing channel is deleted immediately
  • Assuming channel blocks forever after close
2. Which of the following is the correct way to close a channel named ch in Go?
easy
A. ch.Close()
B. ch.close()
C. close channel ch
D. close(ch)

Solution

  1. Step 1: Recall Go syntax for closing channels

    The built-in function close() is used with the channel as argument.
  2. Step 2: Identify correct syntax

    Only close(ch) is valid; methods like ch.close() or ch.Close() do not exist.
  3. Final Answer:

    close(ch) -> Option D
  4. Quick Check:

    Use close(channel) syntax [OK]
Hint: Use built-in close(channel) function [OK]
Common Mistakes:
  • Trying to call close as a method
  • Using incorrect keywords like 'close channel'
  • Capitalizing close as Close
3. What is the output of this Go program?
package main
import "fmt"
func main() {
  ch := make(chan int, 2)
  ch <- 10
  ch <- 20
  close(ch)
  for v := range ch {
    fmt.Print(v, " ")
  }
}
medium
A. 10 20
B. 10 20 0
C. Runtime panic: send on closed channel
D. No output

Solution

  1. Step 1: Understand channel buffering and closing

    Channel has buffer size 2, two values sent, then channel closed.
  2. Step 2: Loop over channel with range

    Range reads all values until channel is closed and drained, printing 10 and 20.
  3. Final Answer:

    10 20 -> Option A
  4. Quick Check:

    Range over closed channel prints buffered values [OK]
Hint: Range prints all buffered values before stopping [OK]
Common Mistakes:
  • Expecting panic on reading closed channel
  • Thinking zero value prints after buffered values
  • Assuming no output because channel is closed
4. Identify the error in this Go code snippet:
ch := make(chan int)
go func() {
  for i := 0; i < 3; i++ {
    ch <- i
  }
  close(ch)
}()
for {
  v, ok := <-ch
  if !ok {
    break
  }
  fmt.Println(v)
}
medium
A. Channel is unbuffered but sender is in goroutine, no deadlock
B. No error, code runs and prints 0 1 2
C. Closing channel inside goroutine causes panic
D. Receiver loop never ends, causing infinite loop

Solution

  1. Step 1: Analyze channel usage and goroutine

    Unbuffered channel with sender in goroutine sends 3 values then closes channel.
  2. Step 2: Check receiver loop behavior

    Receiver reads values until channel closes, then breaks loop correctly.
  3. Final Answer:

    No error, code runs and prints 0 1 2 -> Option B
  4. Quick Check:

    Proper close and ok check ends loop [OK]
Hint: Use ok to detect closed channel and break loop [OK]
Common Mistakes:
  • Thinking closing in goroutine causes panic
  • Assuming infinite loop without break
  • Ignoring need to check ok when receiving
5. Consider this Go program:
package main
import "fmt"
func main() {
  ch := make(chan int)
  go func() {
    for i := 1; i <= 3; i++ {
      ch <- i
    }
    close(ch)
  }()
  for {
    v, ok := <-ch
    if !ok {
      break
    }
    if v%2 == 0 {
      fmt.Println(v)
    }
  }
}
What is the output and why?
hard
A. 2 Only even numbers printed before channel closes
B. 1 2 3 All numbers printed despite channel closing
C. No output because channel is closed before reading
D. Runtime panic due to sending on closed channel

Solution

  1. Step 1: Understand sending and closing in goroutine

    Values 1, 2, 3 sent then channel closed properly.
  2. Step 2: Receiver loop filters even numbers

    Loop reads until closed; prints only values where v%2 == 0, which is 2.
  3. Final Answer:

    2 Only even numbers printed before channel closes -> Option A
  4. Quick Check:

    Filter with condition inside loop after close [OK]
Hint: Filter values after reading until channel closes [OK]
Common Mistakes:
  • Expecting all values printed
  • Thinking channel closes before sending
  • Assuming panic on close with goroutine sender
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