Skip to content
gointermediate

Go Concurrency Quiz

Goroutines, channels, select, sync primitives, and concurrent patterns.

7 questions

By EZ4Code Team

1. How do you start a goroutine?

go doWork()
Use the `go` keyword before the function call
Use `async doWork()`
Use `spawn(doWork)`
Use `thread doWork()`
Explanation: Prefix a function call with `go` to run it as a goroutine: `go doWork()`. Goroutines are lightweight threads managed by the Go runtime, not OS threads. `async` is C#/JS, `spawn` is Erlang/Rust.

2. What is a channel in Go?

A type for sending values between goroutines
A network connection
A logging mechanism
A type of mutex
Explanation: Channels are typed conduits for sending and receiving values between goroutines, enabling safe communication. Created with `make(chan int)`. Go's philosophy: "Don't communicate by sharing memory; share memory by communicating."

3. What does this code do?

ch := make(chan int, 5)
Creates an unbuffered channel
Creates a buffered channel with capacity 5
Creates 5 channels
Creates a channel that sends 5
Explanation: The second argument to `make` sets the buffer capacity. A buffered channel with capacity 5 can hold 5 values without a receiver being ready. Sends block only when the buffer is full; receives block only when the buffer is empty.

4. What does the `select` statement do?

select {
case msg := <-ch1:
    fmt.Println(msg)
case ch2 <- 42:
    fmt.Println("sent")
}
Chooses one ready channel operation at random
Waits for one of multiple channel operations to be ready and executes it
Selects the first ready operation in order
Loops over all channels
Explanation: `select` lets a goroutine wait on multiple channel operations. It blocks until one case is ready, then executes it. If multiple are ready, one is chosen at random. Add a `default` case for non-blocking operation.

5. What does `sync.WaitGroup` do?

var wg sync.WaitGroup
wg.Add(1)
go func() {
    defer wg.Done()
    // work
}()
wg.Wait()
Locks a resource
Waits for a collection of goroutines to finish
Groups goroutines together
Cancels goroutines
Explanation: `WaitGroup` waits for a collection of goroutines to complete. `Add(1)` increments the counter, `Done()` decrements it (usually via `defer`), and `Wait()` blocks until the counter hits zero. For mutual exclusion, use `sync.Mutex`.

6. What happens if you send on a closed channel?

ch := make(chan int)
close(ch)
ch <- 1
Blocks forever
Panics
Returns an error
Silently drops the value
Explanation: Sending on a closed channel causes a runtime panic. Receiving from a closed channel returns the zero value (and a `false` flag if using the two-value receive form). Only the sender should close a channel — never the receiver.

7. What does `go func() { ... }()` do?

Defines a function and runs it as a goroutine immediately
Defines a function but never runs it
Runs the function synchronously
Defines a named function called `func`
Explanation: This is an immediately-invoked function expression (IIFE) preceded by `go`. It defines an anonymous function and runs it as a goroutine. The trailing `()` invokes the function; `go` makes it concurrent. Common for goroutines that need their own scope.

More go Quizzes