Goroutines let your program do many things at once. Understanding their lifecycle helps you manage when they start, run, and stop.
Goroutine lifecycle
go functionName()
Use the go keyword before a function call to start a goroutine.
The goroutine runs independently and does not block the main program.
sayHello function as a goroutine.go sayHello()
go func() {
fmt.Println("Running in a goroutine")
}()This program starts a goroutine that prints a message. The main function waits a little so the goroutine can finish before the program ends.
package main import ( "fmt" "time" ) func sayHello() { fmt.Println("Hello from goroutine") } func main() { go sayHello() // start goroutine time.Sleep(100 * time.Millisecond) // wait a bit to let goroutine run fmt.Println("Main function ends") }
Goroutines run independently but may stop if the main program ends before they finish.
Use synchronization tools like channels or WaitGroups to wait for goroutines to complete.
Starting too many goroutines without control can use a lot of memory.
Goroutines let your program do many things at once.
Use go before a function call to start a goroutine.
Manage their lifecycle carefully to avoid unexpected program exits.
Practice
Solution
Step 1: Understand goroutine purpose
A goroutine lets your program run functions concurrently, meaning at the same time.Step 2: Compare options
Only Run multiple functions at the same time correctly describes this behavior. Other options are incorrect or unrelated.Final Answer:
Run multiple functions at the same time -> Option DQuick Check:
Goroutine = concurrent execution [OK]
- Thinking goroutines slow down the program
- Believing goroutines prevent program exit automatically
- Assuming goroutines fix code errors
doWork()?Solution
Step 1: Recall goroutine syntax
In Go, prefixing a function call withgostarts it as a goroutine.Step 2: Check options
Only go doWork() uses the correct syntax:go doWork(). Others are invalid.Final Answer:
go doWork() -> Option BQuick Check:
Start goroutine = go + function call [OK]
- Using keywords like 'start' or 'run' instead of 'go'
- Writing 'goroutine' as a keyword
- Adding extra words before 'go'
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)
}Solution
Step 1: Understand goroutine and main flow
The goroutine runssayHello()concurrently, printing "Hello from goroutine". The main function prints "Main function finished" immediately.Step 2: Consider the sleep delay
Thetime.Sleeppauses main to allow goroutine to run before program exits, so both prints appear.Final Answer:
Main function finished Hello from goroutine -> Option CQuick Check:
Sleep allows goroutine output after main print [OK]
- Ignoring sleep causes goroutine output to miss
- Assuming goroutine prints before main
- Expecting no output without sleep
package main
import "fmt"
func main() {
go func() {
fmt.Println("Inside goroutine")
}()
}Solution
Step 1: Analyze goroutine lifecycle
The main function starts a goroutine but then immediately exits, ending the program.Step 2: Effect on goroutine execution
Because main exits, the goroutine may not get time to run, so "Inside goroutine" might never print.Final Answer:
The goroutine will never run because main exits immediately -> Option AQuick Check:
Main exit stops goroutine early [OK]
- Thinking goroutine runs after main exits
- Assuming syntax error in anonymous function
- Believing a special import is needed
package main
import (
"fmt"
"time"
)
func main() {
for i := 1; i <= 3; i++ {
// Which option below is correct here?
}
time.Sleep(100 * time.Millisecond)
}Solution
Step 1: Understand variable capture in goroutines
Loop variableichanges during iterations; capturing it directly in goroutine causes all to print last value.Step 2: Use parameter to pass current value
Passingias a parameternto the goroutine fixes this, so each prints its own number.Final Answer:
go func(n int) { fmt.Println(n) }(i) -> Option AQuick Check:
Pass loop variable as parameter to goroutine [OK]
- Capturing loop variable directly causing all prints same value
- Passing wrong variable to goroutine
- Confusing parameter and captured variable
Ready to prove it?
Pass the test and this pattern moves unknown
