Golang Concurrency Goroutines Quiz
40 comprehensive questions on Golang goroutines, covering goroutine creation, spawning, stack management, leaks, and best practices — with 18 code examples demonstrating concurrent programming patterns and common pitfalls.
Question 1
What is a goroutine in Golang?
Question 2
How do you create a goroutine?
go func() {
fmt.Println("Hello from goroutine")
}()Question 3
What happens when the main function ends in a Go program?
package main
import "fmt"
func main() {
go func() {
fmt.Println("This might not print")
}()
// main ends here
}Question 4
What is the initial stack size of a goroutine?
Question 5
How do goroutines communicate?
Question 6
What is a goroutine leak?
Question 7
How can you prevent goroutine leaks?
go func() {
defer func() { /* cleanup */ }()
for {
select {
case <-done:
return
default:
// do work
}
}
}()Question 8
What is the GOMAXPROCS setting?
Question 9
How do you pass data to a goroutine?
func worker(data string) {
fmt.Println(data)
}
func main() {
go worker("hello")
}Question 10
What happens when you modify a shared variable from multiple goroutines?
var counter int
go func() { counter++ }()
go func() { counter++ }()Question 11
What is the difference between goroutines and OS threads?
Question 12
How do you wait for a goroutine to finish?
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
// do work
}()
wg.Wait()Question 13
What is stack splitting in goroutines?
Question 14
How do you handle panics in goroutines?
go func() {
defer func() {
if r := recover(); r != nil {
// handle panic
}
}()
// risky code
}()Question 15
What is the runtime.Gosched() function?
Question 16
How do you limit the number of concurrent goroutines?
semaphore := make(chan struct{}, maxGoroutines)
for i := 0; i < n; i++ {
go func() {
semaphore <- struct{}{} // acquire
defer func() { <-semaphore }() // release
// do work
}()
}Question 17
What is a goroutine's lifecycle?
Question 18
How do you get the number of currently running goroutines?
import "runtime"
goroutines := runtime.NumGoroutine()Question 19
What happens when a goroutine calls runtime.Goexit()?
Question 20
How do you implement a worker pool pattern?
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
results <- j * 2
}
}
func main() {
jobs := make(chan int, 100)
results := make(chan int, 100)
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
for a := 1; a <= 5; a++ {
<-results
}
}Question 21
What is the problem with this code?
for i := 0; i < 10; i++ {
go func() {
fmt.Println(i)
}()
}Question 22
How do you fix the loop variable capture issue?
for i := 0; i < 10; i++ {
go func(i int) {
fmt.Println(i)
}(i)
}Question 23
What is cooperative scheduling in Go?
Question 24
How do you detect goroutine leaks in testing?
func TestNoLeak(t *testing.T) {
before := runtime.NumGoroutine()
// test code that spawns goroutines
time.Sleep(100 * time.Millisecond) // wait for cleanup
after := runtime.NumGoroutine()
if after > before {
t.Errorf("Goroutine leak: %d before, %d after", before, after)
}
}Question 25
What is the maximum number of goroutines you can create?
Question 26
How do you implement a timeout for a goroutine?
select {
case result := <-ch:
// use result
case <-time.After(5 * time.Second):
// timeout
}Question 27
What is the difference between buffered and unbuffered channels in goroutines?
Question 28
How do you gracefully shutdown multiple goroutines?
done := make(chan struct{})
for i := 0; i < n; i++ {
go worker(done)
}
// To shutdown:
close(done)Question 29
What is fan-out pattern in goroutines?
Question 30
What is fan-in pattern?
Question 31
How do you implement fan-in?
func fanIn(ch1, ch2 <-chan int) <-chan int {
c := make(chan int)
go func() { for { c <- <-ch1 } }()
go func() { for { c <- <-ch2 } }()
return c
}Question 32
What is the problem with too many goroutines?
Question 33
How do you debug deadlocks in goroutines?
Question 34
What is a context in goroutine management?
Question 35
How do you use context for goroutine cancellation?
ctx, cancel := context.WithCancel(context.Background())
go func() {
select {
case <-ctx.Done():
return
default:
// do work
}
}()
// Later: cancel()Question 36
What is the best practice for error handling in goroutines?
results := make(chan result, n)
for i := 0; i < n; i++ {
go func() {
res, err := doWork()
results <- result{res, err}
}()
}
// Collect results and handle errorsQuestion 37
How do you implement a pipeline with goroutines?
func generator() <-chan int {
ch := make(chan int)
go func() {
for i := 0; i < 10; i++ {
ch <- i
}
close(ch)
}()
return ch
}
func main() {
for num := range generator() {
fmt.Println(num)
}
}Question 38
What is the GODEBUG environment variable used for?
Question 39
How do you avoid goroutine leaks in HTTP servers?
func handler(w http.ResponseWriter, r *http.Request) {
done := make(chan struct{})
go func() {
defer close(done)
// long running work
}()
select {
case <-done:
// work completed
case <-r.Context().Done():
// request cancelled
return
}
}Question 40
What is the most important principle for goroutine usage?
