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?
Aasync
Bgo
Cthread
Dspawn
✗ Incorrect
The go keyword is used to start a new goroutine.
What happens if the main function ends before goroutines finish?
AProgram exits immediately, stopping goroutines
BProgram waits for all goroutines automatically
CGoroutines continue running in background
DCompiler throws an error
✗ Incorrect
The program exits immediately, stopping all running goroutines.
Which is the safest way for goroutines to communicate?
AUsing files
BGlobal variables without locks
CPrinting to console
DChannels
✗ Incorrect
Channels provide safe communication between goroutines avoiding race conditions.
What is a goroutine best described as?
AA lightweight thread managed by Go runtime
BA heavyweight OS thread
CA function that runs only once
DA type of variable
✗ Incorrect
Goroutines are lightweight threads managed by the Go runtime.
How do you ensure a goroutine has time to finish before main exits?
ACall <code>exit()</code> after goroutine
BNothing, it finishes automatically
CUse time.Sleep or synchronization methods
DUse the <code>wait</code> keyword
✗ 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.
Practice
(1/5)
1. What does adding go before a function call do in Go?
easy
A. Starts the function as a new goroutine running concurrently
B. Delays the function call until later
C. Runs the function only if a condition is true
D. Converts the function into a method
Solution
Step 1: Understand the role of go keyword
The go keyword in Go is used to start a new goroutine, which is a lightweight thread that runs concurrently with other goroutines.
Step 2: Identify what happens when go is used before a function call
When go is placed before a function call, that function runs asynchronously, allowing the program to continue without waiting for it to finish.
Final Answer:
Starts the function as a new goroutine running concurrently -> Option A
Quick Check:
go starts concurrent execution [OK]
Hint: Remember: 'go' runs function concurrently [OK]
Common Mistakes:
Thinking 'go' delays function execution
Confusing goroutine with method conversion
Assuming 'go' runs function conditionally
2. Which of the following is the correct syntax to start a goroutine that calls function doWork()?
easy
A. run go doWork()
B. go doWork()
C. start doWork()
D. goroutine doWork()
Solution
Step 1: Recall the syntax for starting a goroutine
In Go, to start a goroutine, you simply write go followed by the function call without parentheses or extra keywords.
Step 2: Match the correct syntax
go doWork() uses go doWork(), which is the exact syntax to start a goroutine. Other options use invalid keywords or extra words.
Final Answer:
go doWork() -> Option B
Quick Check:
Correct goroutine syntax = go function() [OK]
Hint: Use 'go' directly before function call [OK]
Common Mistakes:
Adding extra keywords like 'start' or 'run'
Writing 'goroutine' instead of 'go'
Missing parentheses after function name
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("Hello from main")
time.Sleep(100 * time.Millisecond)
}
medium
A. Hello from main\nHello from goroutine
B. Hello from goroutine\nHello from main
C. Hello from main only
D. No output
Solution
Step 1: Understand goroutine and main execution order
The sayHello function runs as a goroutine, so it runs concurrently with the main function. The main function prints first, then sleeps to allow the goroutine to run.
Step 2: Analyze output order
Main prints "Hello from main" immediately. The goroutine prints "Hello from goroutine" during the sleep period, so both lines appear with main's message first.
Final Answer:
Hello from main
Hello from goroutine -> Option A
Quick Check:
main prints first, goroutine runs during sleep [OK]
Hint: Sleep to let goroutine print after main [OK]
Common Mistakes:
Assuming goroutine always prints first
Forgetting to add sleep to wait for goroutine
Expecting no output because goroutine is async
4. Identify the error in this Go code snippet that tries to start a goroutine:
B. Function printNumber cannot be used as goroutine
C. Missing parentheses after function name in goroutine call
D. Missing import of fmt package
Solution
Step 1: Check goroutine syntax for function calls
When starting a goroutine, the function must be called with parentheses, even if it has parameters. Here, go printNumber misses parentheses and argument.
Step 2: Identify the specific syntax error
The code should be go printNumber(5) or some integer argument with parentheses to call the function properly as a goroutine.
Final Answer:
Missing parentheses after function name in goroutine call -> Option C
Quick Check:
Function call needs parentheses [OK]
Hint: Always use parentheses with goroutine function calls [OK]
Common Mistakes:
Omitting parentheses when calling function as goroutine
Thinking goroutines can't have parameters
Ignoring missing imports causing unrelated errors
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 all printing 4?
for i := 1; i <= 3; i++ {
go func() {
fmt.Println(i)
}()
}
hard
A. Add time.Sleep inside the loop after starting goroutine
B. Use global variable instead of loop variable
C. Remove the anonymous function and call go fmt.Println(i)
D. Pass i as parameter to anonymous function: go func(n int) { fmt.Println(n) }(i)