Dev Skills
Go tests·10 questions

Checkpoint 3

Concurrency mixed with what came before: goroutines, channels, locks, context and the basics they lean on.

1 / 10

main runs go fmt.Println("hi") and then returns straight away. What happens?

All questions with explanations (answers shown)
  1. 1. main runs go fmt.Println("hi") and then returns straight away. What happens?

    Answer: Most likely nothing prints, because the program exits when main returns

    The program ends as soon as main returns and does not wait for other goroutines. Use a sync.WaitGroup or a channel to wait.

  2. 2. ch := make(chan int), then ch <- 1 in main with no other goroutines. What happens?

    Answer: fatal error: all goroutines are asleep - deadlock!

    A send on an unbuffered channel waits for a receiver. No goroutine can ever receive, so the runtime notices and stops the program.

  3. 3. ch := make(chan int, 1); ch <- 7; close(ch); a := <-ch; b, ok := <-ch. What are a, b and ok?

    Answer: 7 0 false

    Closing does not throw away buffered values, so a gets 7. After that the channel is empty and closed, so receives give the zero value and ok is false.

  4. 4. Which command reports data races while your tests run?

    Answer: go test -race

    The race detector is built in when you pass -race. It only sees races that actually happen during the run, so tests need to exercise the concurrent paths.

  5. 5. After ctx, cancel := context.WithCancel(parent) and cancel(), what does ctx.Err() return?

    Answer: context.Canceled

    Once a context is done, Err says why. A manual cancel gives context.Canceled; a passed deadline or timeout gives context.DeadlineExceeded.

  6. 6. Why call wg.Add(1) before the go statement instead of inside the new goroutine?

    Answer: Wait could run before the goroutine calls Add, see a zero counter and return early

    Wait returns when the counter is zero. If the goroutine has not started yet, the counter may still be zero. Adding first removes that race.

  7. 7. ch is an empty channel. What does select { case v := <-ch: fmt.Print(v); default: fmt.Print("none") } do?

    Answer: Prints none right away

    A select with a default never blocks. If no case is ready at that moment, default runs.

  8. 8. Several goroutines write to the same plain map with no lock. What can happen?

    Answer: The program can crash with fatal error: concurrent map writes

    Built-in maps are not safe for concurrent writes, and the runtime crashes the program when it catches it. Guard the map with a sync.Mutex, or use sync.Map for the cases it fits.

  9. 9. What does mu.Lock(); defer mu.Unlock() at the top of a function give you?

    Answer: The lock is released when the function returns, including early returns and panics

    Deferred calls run when the surrounding function returns, however it returns. That is why the unlock sits right next to the lock.

  10. 10. On Go 1.22 or later, for i := 0; i < 3; i++ { wg.Add(1); go func() { defer wg.Done(); fmt.Print(i) }() } followed by wg.Wait(). What prints?

    Answer: Each of 0, 1 and 2 once, in any order

    Since Go 1.22 every loop iteration gets its own i, so each goroutine sees a different value. The scheduler decides who runs first, so the order is not fixed.