Concurrent execution lets your program do many things at the same time. This helps it run faster and handle multiple tasks smoothly.
Concurrent execution model in Go
go func() {
// code to run concurrently
}()This syntax starts a new goroutine, which is a lightweight thread.
The code inside the function runs at the same time as the rest of the program.
go func() {
println("Hello from goroutine")
}()func sayHello() {
println("Hello")
}
func main() {
go sayHello()
println("Main function")
}for i := 1; i <= 3; i++ { go func(n int) { println("Goroutine", n) }(i) }
This program runs printNumbers in a goroutine while main prints messages. The sleep in main lets the goroutine finish printing.
package main import ( "fmt" "time" ) func printNumbers() { for i := 1; i <= 5; i++ { fmt.Println("Number", i) time.Sleep(100 * time.Millisecond) } } func main() { go printNumbers() // run concurrently fmt.Println("Main function continues") time.Sleep(600 * time.Millisecond) // wait to see goroutine output fmt.Println("Main function ends") }
Goroutines are very lightweight compared to traditional threads.
Use time.Sleep or synchronization to let goroutines finish before main ends.
Channels are often used to communicate between goroutines (not shown here).
Concurrent execution lets your program do multiple things at once.
In Go, goroutines are used to run code concurrently with simple syntax.
Remember to manage timing so goroutines have time to run before the program exits.
Practice
Solution
Step 1: Understand goroutine concept
A goroutine is a function or method that runs concurrently with other goroutines.Step 2: Identify goroutine characteristics
It is lightweight and managed by Go's runtime, unlike OS threads which are heavier.Final Answer:
A lightweight thread managed by the Go runtime -> Option AQuick Check:
Goroutine = lightweight thread [OK]
- Confusing goroutine with OS thread
- Thinking goroutine is a variable type
- Assuming goroutine runs only once
doWork()?Solution
Step 1: Recall goroutine syntax
In Go, to start a goroutine, prefix the function call with the keywordgo.Step 2: Match syntax with options
Onlygo doWork()uses the correct keyword and syntax.Final Answer:
go doWork() -> Option AQuick Check:
Start goroutine with 'go' keyword [OK]
- Using 'goroutine' keyword instead of 'go'
- Using 'start' or 'run' which are invalid
- Omitting the 'go' keyword
package main
import (
"fmt"
"time"
)
func main() {
go fmt.Println("Hello from goroutine")
fmt.Println("Hello from main")
time.Sleep(100 * time.Millisecond)
}Solution
Step 1: Understand goroutine and main execution
The goroutine prints "Hello from goroutine" concurrently, while main prints "Hello from main" immediately.Step 2: Analyze timing with Sleep
Thetime.Sleepallows the goroutine time to run before the program exits, so both lines print.Final Answer:
Hello from main\nHello from goroutine -> Option DQuick Check:
Sleep allows goroutine output after main [OK]
- Ignoring need for sleep to let goroutine run
- Assuming goroutine output always prints first
- Expecting no output due to race condition
package main
import "fmt"
func main() {
go func() {
fmt.Println("Running")
}()
}Solution
Step 1: Check syntax correctness
The anonymous function syntax and goroutine start are correct; no syntax errors.Step 2: Analyze program flow
The main function exits immediately after starting the goroutine, so "Running" may never print.Final Answer:
Program exits before goroutine runs -> Option BQuick Check:
Program ends before goroutine runs [OK]
- Thinking syntax is wrong for anonymous goroutine
- Forgetting program ends immediately
- Assuming goroutine blocks main automatically
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 1; i <= 3; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
fmt.Println(n)
}(i)
}
// What should be here?
}Solution
Step 1: Understand WaitGroup usage
WaitGroup tracks goroutines;wg.Add(1)before start,wg.Done()after done.Step 2: Ensure main waits for all goroutines
Callingwg.Wait()blocks main until all goroutines callDone().Final Answer:
wg.Wait() -> Option CQuick Check:
WaitGroup.Wait() waits for goroutines [OK]
- Using Sleep instead of WaitGroup for sync
- Trying to close WaitGroup (not allowed)
- Calling Done() in main instead of goroutine
Ready to prove it?
Pass the test and this pattern moves unknown
