Bird
Raised Fist0
Goprogramming~20 mins

Goroutine lifecycle - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Goroutine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Goroutine execution order
What is the output of this Go program?
Go
package main

import (
	"fmt"
	"time"
)

func main() {
	go fmt.Println("Hello from goroutine")
	fmt.Println("Hello from main")
	time.Sleep(100 * time.Millisecond)
}
AHello from main\nHello from goroutine
BHello from goroutine\nHello from main
CHello from main
DNo output (program exits immediately)
Attempts:
2 left
💡 Hint
Remember that goroutines run concurrently and main may finish before they print unless you wait.
❓ Predict Output
intermediate
2:00remaining
Goroutine variable capture
What will be printed by this Go program?
Go
package main

import (
	"fmt"
	"time"
)

func main() {
	for i := 0; i < 3; i++ {
		go func(i int) {
			fmt.Println(i)
		}(i)
	}
	time.Sleep(100 * time.Millisecond)
}
A0\n0\n0
B3\n3\n3
CCompilation error
D0\n1\n2
Attempts:
2 left
💡 Hint
Think about how the loop variable is captured by the goroutine's closure.
🔧 Debug
advanced
2:00remaining
Detecting goroutine leak
What problem does this Go code cause?
Go
package main

import (
	"fmt"
	"time"
)

func worker(ch chan int) {
	for {
		val, ok := <-ch
		if !ok {
			return
		}
		fmt.Println(val)
	}
}

func main() {
	ch := make(chan int)
	go worker(ch)
	ch <- 1
	ch <- 2
	// Missing close(ch)
	select {}
}
AThe program deadlocks because the channel is never closed and worker waits forever.
BThe program prints 1 and 2 and exits cleanly.
CCompilation error due to missing channel close.
DThe program panics at runtime.
Attempts:
2 left
💡 Hint
Think about what happens when the worker goroutine waits on a channel that never closes.
❓ Predict Output
advanced
2:00remaining
Goroutine synchronization with WaitGroup
What is the output of this Go program?
Go
package main

import (
	"fmt"
	"sync"
)

func main() {
	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		fmt.Println("First goroutine")
	}()
	go func() {
		defer wg.Done()
		fmt.Println("Second goroutine")
	}()
	wg.Wait()
	fmt.Println("All done")
}
ACompilation error due to missing WaitGroup initialization
BAll done\nFirst goroutine\nSecond goroutine
CFirst goroutine\nSecond goroutine\nAll done
DFirst goroutine\nAll done\nSecond goroutine
Attempts:
2 left
💡 Hint
WaitGroup waits for all goroutines to finish before continuing.
🧠 Conceptual
expert
2:00remaining
Goroutine lifecycle states
Which of the following correctly describes the lifecycle states of a goroutine?
ANew, Runnable, Blocked, Terminated
BCreated, Running, Waiting, Dead
CStart, Execute, Sleep, Stop
DInit, Active, Paused, Finished
Attempts:
2 left
💡 Hint
Think about the main states a goroutine goes through from creation to termination.

Practice

(1/5)
1. What does a goroutine in Go allow your program to do?
easy
A. Make the program run slower
B. Automatically fix bugs in the code
C. Prevent the program from exiting
D. Run multiple functions at the same time

Solution

  1. Step 1: Understand goroutine purpose

    A goroutine lets your program run functions concurrently, meaning at the same time.
  2. Step 2: Compare options

    Only Run multiple functions at the same time correctly describes this behavior. Other options are incorrect or unrelated.
  3. Final Answer:

    Run multiple functions at the same time -> Option D
  4. Quick Check:

    Goroutine = concurrent execution [OK]
Hint: Goroutines run functions concurrently, like multitasking [OK]
Common Mistakes:
  • Thinking goroutines slow down the program
  • Believing goroutines prevent program exit automatically
  • Assuming goroutines fix code errors
2. Which of the following is the correct syntax to start a goroutine for function doWork()?
easy
A. goroutine doWork()
B. go doWork()
C. start doWork()
D. run go doWork()

Solution

  1. Step 1: Recall goroutine syntax

    In Go, prefixing a function call with go starts it as a goroutine.
  2. Step 2: Check options

    Only go doWork() uses the correct syntax: go doWork(). Others are invalid.
  3. Final Answer:

    go doWork() -> Option B
  4. Quick Check:

    Start goroutine = go + function call [OK]
Hint: Use 'go' before function call to start goroutine [OK]
Common Mistakes:
  • Using keywords like 'start' or 'run' instead of 'go'
  • Writing 'goroutine' as a keyword
  • Adding extra words before 'go'
3. What will be the output of this Go program?
package main
import (
  "fmt"
  "time"
)
func sayHello() {
  fmt.Println("Hello from goroutine")
}
func main() {
  go sayHello()
  fmt.Println("Main function finished")
  time.Sleep(100 * time.Millisecond)
}
medium
A. Main function finished
B. Hello from goroutine\nMain function finished
C. Main function finished\nHello from goroutine
D. No output

Solution

  1. Step 1: Understand goroutine and main flow

    The goroutine runs sayHello() concurrently, printing "Hello from goroutine". The main function prints "Main function finished" immediately.
  2. Step 2: Consider the sleep delay

    The time.Sleep pauses main to allow goroutine to run before program exits, so both prints appear.
  3. Final Answer:

    Main function finished Hello from goroutine -> Option C
  4. Quick Check:

    Sleep allows goroutine output after main print [OK]
Hint: Sleep main to let goroutine print before exit [OK]
Common Mistakes:
  • Ignoring sleep causes goroutine output to miss
  • Assuming goroutine prints before main
  • Expecting no output without sleep
4. Identify the problem in this Go code snippet:
package main
import "fmt"
func main() {
  go func() {
    fmt.Println("Inside goroutine")
  }()
}
medium
A. The goroutine will never run because main exits immediately
B. Syntax error in anonymous function
C. Missing import for goroutine package
D. No problem, code runs fine

Solution

  1. Step 1: Analyze goroutine lifecycle

    The main function starts a goroutine but then immediately exits, ending the program.
  2. Step 2: Effect on goroutine execution

    Because main exits, the goroutine may not get time to run, so "Inside goroutine" might never print.
  3. Final Answer:

    The goroutine will never run because main exits immediately -> Option A
  4. Quick Check:

    Main exit stops goroutine early [OK]
Hint: Main must wait or goroutine may not run [OK]
Common Mistakes:
  • Thinking goroutine runs after main exits
  • Assuming syntax error in anonymous function
  • Believing a special import is needed
5. You want to start 3 goroutines that each print their number (1, 2, 3). Which code correctly ensures each goroutine prints its own number without race conditions?
package main
import (
  "fmt"
  "time"
)
func main() {
  for i := 1; i <= 3; i++ {
    // Which option below is correct here?
  }
  time.Sleep(100 * time.Millisecond)
}
hard
A. go func(n int) { fmt.Println(n) }(i)
B. go func() { fmt.Println(i) }(i)
C. go func() { fmt.Println(i) }()
D. go func(n int) { fmt.Println(i) }(i)

Solution

  1. Step 1: Understand variable capture in goroutines

    Loop variable i changes during iterations; capturing it directly in goroutine causes all to print last value.
  2. Step 2: Use parameter to pass current value

    Passing i as a parameter n to the goroutine fixes this, so each prints its own number.
  3. Final Answer:

    go func(n int) { fmt.Println(n) }(i) -> Option A
  4. Quick Check:

    Pass loop variable as parameter to goroutine [OK]
Hint: Pass loop variable as function parameter to avoid capture issues [OK]
Common Mistakes:
  • Capturing loop variable directly causing all prints same value
  • Passing wrong variable to goroutine
  • Confusing parameter and captured variable
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