Recall & Review
beginner
What is a goroutine in Go?
A goroutine is a lightweight thread managed by the Go runtime. It allows functions to run concurrently without the overhead of traditional threads.
Click to reveal answer
beginner
How do you start a new goroutine?
Use the keyword
go before a function call. For example: go myFunction() starts myFunction as a new goroutine.Click to reveal answer
intermediate
What happens if the main function finishes before goroutines complete?
The program exits immediately, and any running goroutines are stopped. You need to synchronize or wait for goroutines to finish to see their output.
Click to reveal answer
intermediate
Can goroutines share memory? How should they communicate?
Goroutines can share memory, but it is safer to communicate using channels to avoid race conditions and ensure safe data exchange.
Click to reveal answer
beginner
Write a simple example to create a goroutine that prints "Hello from goroutine!".
```go
package main
import (
"fmt"
"time"
)
func main() {
go func() {
fmt.Println("Hello from goroutine!")
}()
time.Sleep(100 * time.Millisecond) // Wait to let goroutine finish
}
```Click to reveal answer
Which keyword starts a new goroutine in Go?
✗ Incorrect
The
go keyword is used to start a new goroutine.What happens if the main function ends before goroutines finish?
✗ Incorrect
The program exits immediately, stopping all running goroutines.
Which is the safest way for goroutines to communicate?
✗ Incorrect
Channels provide safe communication between goroutines avoiding race conditions.
What is a goroutine best described as?
✗ Incorrect
Goroutines are lightweight threads managed by the Go runtime.
How do you ensure a goroutine has time to finish before main exits?
✗ Incorrect
You can use
time.Sleep or synchronization tools like WaitGroups to wait for goroutines.Explain how to create and run a goroutine in Go and why it is useful.
Think about how Go lets you run tasks at the same time easily.
You got /4 concepts.
Describe what happens if the main function finishes before goroutines complete and how to handle it.
Consider the program lifecycle and waiting for tasks.
You got /4 concepts.