Goroutines Questions with Answers

11 practice questions on Goroutines (Go) with answers and step-by-step solutions. Try 10 samples below, then practise all 11 free in the AptiNest app.

Sample questions

Question 1Easy

How do you start a goroutine?

  1. Wrap the function in a channel
  2. Call runtime.NewThread(f) and then thread.Start() on the returned handle
  3. Put the go keyword before a function call
  4. Use the async keyword on a function

Answer: C. Put the go keyword before a function call

Explanation

go f(x) runs f concurrently; goroutines are cheap, runtime-scheduled lightweight threads.

Step-by-step solution

  1. Use the go keyword before a function call to start a goroutine.
  2. go f() launches f in a new goroutine, allowing it to run concurrently.
  3. This is the standard way to create and start a goroutine in Go.
  4. Other options like async or runtime functions are not correct syntax for goroutines.
  5. The go keyword ensures the function runs in parallel without blocking the main thread.

Why the other options are wrong

  • A. A channel does not start a goroutine; channels are for communication.
  • B. runtime.NewThread is not a Go API; goroutines start with the go keyword.
  • D. async is not a Go keyword; Go has no async modifier for starting work.

Tip: Use go before a function call to start a goroutine; it's the only correct way in Go.

Question 2Easy

What does the -race flag (e.g. go test -race) do?

  1. Checks the whole program for deadlocks and data races at compile time, without running it
  2. Instruments the program to detect data races at runtime
  3. Makes goroutines run faster
  4. Compiles without garbage collection

Answer: B. Instruments the program to detect data races at runtime

Explanation

The race detector reports unsynchronised concurrent accesses it observes while the code runs.

Step-by-step solution

  1. The -race flag builds with the race detector.
  2. It watches memory accesses while the program runs.
  3. It reports data races found at runtime.

Why the other options are wrong

  • A. It runs the program; it is not compile-time.
  • C. It slows execution.
  • D. It does not disable GC.

Tip: -race = runtime detector.

Question 3Easy

Which is the idiomatic way to wait for a set of goroutines to finish?

  1. time.Sleep with a guessed duration
  2. runtime.Gosched in main
  3. An infinite for loop
  4. sync.WaitGroup

Answer: D. sync.WaitGroup

Explanation

Add before starting each goroutine, Done when it finishes, and Wait in the caller.

Step-by-step solution

  1. Create a sync.WaitGroup variable before launching goroutines.
  2. Call wg.Add(n) where n equals the number of goroutines.
  3. In each goroutine defer wg.Done() to signal its finish.
  4. sync.WaitGroup.

Why the other options are wrong

  • A. time.Sleep with a guessed duration is unreliable, since the goroutines may take longer or shorter.
  • B. runtime.Gosched only yields the processor once and does not wait for goroutines to finish.
  • C. An infinite for loop burns CPU and never ends, so main can never move on past it.

Tip: Use wg.Add, wg.Done, and wg.Wait for clean synchronization.

Question 4Medium

What is the output?

package main

import "fmt"

func main() {
	go fmt.Println("from goroutine")
}
  1. Usually prints nothing
  2. Always prints from goroutine
  3. Deadlock panic
  4. Compile error

Answer: A. Usually prints nothing

Explanation

When main returns, the program exits without waiting for other goroutines, so the print usually never runs.

Step-by-step solution

  1. The go keyword starts a new goroutine for the print function.
  2. The main function finishes immediately without waiting.
  3. The program exits before the goroutine can execute.
  4. Usually prints nothing.

Why the other options are wrong

  • B. Always printing is wrong: main can exit first, so the goroutine may never run.
  • C. No deadlock: main does not block on anything, so Go reports none.
  • D. The code compiles; fmt.Println is a valid call after the go keyword.

Tip: Use time.Sleep or channels to wait for goroutines in short examples.

Question 5Medium

What is a data race in Go?

  1. Two goroutines access the same variable concurrently, at least one writing, unsynchronised
  2. A goroutine running faster than main
  3. A channel with too many readers
  4. Two goroutines finishing at the same moment, so the scheduler cannot decide which result to keep

Answer: A. Two goroutines access the same variable concurrently, at least one writing, unsynchronised

Explanation

Races cause unpredictable results; run tests with go test -race to detect them.

Step-by-step solution

  1. A data race needs concurrent access.
  2. At least one access is a write.
  3. There is no synchronisation.

Why the other options are wrong

  • B. Speed is irrelevant.
  • C. Channel readers are not races.
  • D. Finish timing is not the definition.

Tip: Race = unsynchronised concurrent write.

Question 6Medium

Why are goroutines cheaper than OS threads?

  1. They are compiled into separate lightweight processes that share no memory with each other
  2. They never use memory
  3. They start with small growable stacks and are multiplexed onto few threads by the runtime
  4. They run without a scheduler

Answer: C. They start with small growable stacks and are multiplexed onto few threads by the runtime

Explanation

Hundreds of thousands of goroutines are practical; the runtime scheduler maps them onto OS threads.

Step-by-step solution

  1. Goroutines start with small stacks that grow as needed.
  2. The Go runtime manages scheduling, not the OS.
  3. This reduces overhead compared to OS thread creation.
  4. Fewer resources are used per goroutine than per thread.
  5. Runtime multiplexing allows many goroutines on few threads.
  6. So, goroutines are cheaper than OS threads.

Why the other options are wrong

  • A. Goroutines are not separate processes; they run inside one program and share its memory.
  • B. Goroutines use memory for their stacks and state, so this claim is false.
  • D. Goroutines still need a scheduler; the Go runtime provides it.

Tip: Use goroutines for high-concurrency tasks to save resources.

Question 7Medium

What is the output?

package main

import (
	"fmt"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	total := 0
	var mu sync.Mutex
	for i := 1; i <= 4; i++ {
		wg.Add(1)
		go func(n int) {
			defer wg.Done()
			mu.Lock()
			total += n
			mu.Unlock()
		}(i)
	}
	wg.Wait()
	fmt.Println(total)
}
  1. 4
  2. 10
  3. 0
  4. Varies between runs

Answer: B. 10

Explanation

The mutex protects total, and Wait blocks until all four goroutines finish: 1+2+3+4 = 10.

Step-by-step solution

  1. Each goroutine adds its number to total using a mutex.
  2. Four goroutines run with values 1, 2, 3, and 4.
  3. Mutex ensures only one goroutine modifies total at a time.
  4. Total becomes 1 + 2 + 3 + 4 = 10.
  5. WaitGroup waits for all goroutines to finish.
  6. Final value of total is printed as 10.

Why the other options are wrong

  • A. Each goroutine adds its own number, so the total is not the count 4; the values are summed.
  • C. total starts at 0, but Wait returns only after all four goroutines add, so it is not 0.
  • D. The mutex keeps the sum correct, so the result is always 10 and does not vary.

Tip: Use mutexes to prevent race conditions when multiple goroutines update a shared variable.

Question 8Medium

Why should wg.Add(1) be called before the go statement rather than inside the goroutine?

  1. It makes goroutines start faster
  2. Add panics when called inside a goroutine, because the counter belongs to the parent
  3. Otherwise Wait might run before Add and return too early
  4. Done must be called before Add

Answer: C. Otherwise Wait might run before Add and return too early

Explanation

If the goroutine has not been scheduled yet, Wait sees a zero counter and returns immediately.

Step-by-step solution

  1. wg.Add(1) sets the wait group counter to 1 before starting the goroutine.
  2. If Add is called inside the goroutine, Wait might see zero and return early.
  3. Wait needs to know the correct count to block until all goroutines finish.
  4. Calling Add before go ensures the counter is set properly for Wait.
  5. This prevents Wait from returning too early and missing goroutines.

Why the other options are wrong

  • A. Starting goroutines faster is not the reason; the order only matters for the counter.
  • B. Add does not panic inside a goroutine; it is legal, but racing it against Wait is the bug.
  • D. Done is not required to come before Add; it lowers the counter when a goroutine finishes.

Tip: Always call Add before go to avoid premature return from Wait.

Question 9Hard

What does GOMAXPROCS control?

  1. The maximum number of goroutines the program may create before the runtime starts to block
  2. The size of each goroutine's stack
  3. The number of channels allowed
  4. How many OS threads can execute Go code simultaneously

Answer: D. How many OS threads can execute Go code simultaneously

Explanation

It defaults to the number of available CPUs; you can have many more goroutines than that.

Step-by-step solution

  1. GOMAXPROCS sets limits on parallelism in Go programs.
  2. It controls how many OS threads can run Go code at the same time.
  3. This affects the program's ability to use multiple CPUs efficiently.
  4. The value is usually set to the number of available CPUs by default.
  5. Changing it can improve or reduce performance based on system resources.
  6. Final answer: How many OS threads can execute Go code simultaneously.

Why the other options are wrong

  • A. GOMAXPROCS does not cap goroutine count; a program can create many goroutines regardless.
  • B. Stack size is set by the runtime as goroutines grow, not by GOMAXPROCS.
  • C. Channels are not limited by GOMAXPROCS; it only sets how many threads run Go code.

Tip: Set GOMAXPROCS to match your CPU cores for optimal performance in multi-threaded Go apps.

Question 10Hard

What is a goroutine leak?

  1. A goroutine that prints to stderr
  2. A goroutine blocked forever, e.g. on a channel nobody will send to
  3. A goroutine that finishes too quickly
  4. Memory that a goroutine frees twice, corrupting the heap and causing a crash later

Answer: B. A goroutine blocked forever, e.g. on a channel nobody will send to

Explanation

Leaked goroutines hold memory forever; use context cancellation or closed channels so they can exit.

Step-by-step solution

  1. A goroutine runs concurrently like a lightweight thread.
  2. It may wait on a channel for a value.
  3. If the channel never receives, the goroutine cannot proceed.
  4. It remains blocked forever, keeping its stack memory.
  5. A goroutine leak is a goroutine blocked forever, e.g. on a channel nobody will send to.

Why the other options are wrong

  • A. Printing to stderr is just logging; it does not leak or block a goroutine by itself.
  • C. A goroutine that finishes quickly is not a leak; finishing is normal.
  • D. Go frees memory with its garbage collector, so a double free is not a goroutine leak.

Tip: Close channels or use context cancellation to prevent goroutine leaks.

Practise all 11 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Go topics · All topics