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 goroutine?
Use the keyword go before a function call, like go myFunction(), to start it as a goroutine.
Click to reveal answer
intermediate
What is a channel in Go's concurrent model?
A channel is a typed conduit through which goroutines communicate by sending and receiving values, helping to synchronize execution.
Click to reveal answer
beginner
Why is concurrency important in Go?
Concurrency lets Go programs handle multiple tasks at once, improving efficiency and responsiveness, especially for I/O or network operations.
Click to reveal answer
intermediate
What happens if a goroutine tries to send on a channel but no one is receiving?
The goroutine blocks (waits) until another goroutine receives from the channel, ensuring safe communication without data loss.
Click to reveal answer
How do you create a new goroutine in Go?
ABy using 'thread.start()'
BBy calling 'new goroutine()'
CBy importing 'concurrent' package
DBy prefixing a function call with 'go'
✗ Incorrect
In Go, you start a goroutine by prefixing a function call with the keyword 'go'.
What is the main purpose of channels in Go?
ATo store data permanently
BTo create new goroutines
CTo communicate and synchronize between goroutines
DTo handle errors
✗ Incorrect
Channels allow goroutines to send and receive data, enabling communication and synchronization.
What happens when a goroutine sends data on an unbuffered channel with no receiver?
AThe program crashes
BThe goroutine blocks until another goroutine receives
CThe data is lost
DThe data is queued automatically
✗ Incorrect
Sending on an unbuffered channel blocks the sender until a receiver is ready.
Which Go feature helps run multiple functions at the same time?
AGoroutines
BInterfaces
CStructs
DMaps
✗ Incorrect
Goroutines allow concurrent execution of functions.
What keyword is used to receive data from a channel?
A<-
Bgo
Cchan
Dsend
✗ Incorrect
The '<-' operator is used to send or receive data on channels.
Explain how goroutines and channels work together in Go's concurrent execution model.
Think about how two friends pass notes to coordinate their work.
You got /4 concepts.
Describe what happens when you start a goroutine and how it differs from a normal function call.
Imagine starting a new task that runs alongside your current one.
You got /4 concepts.
Practice
(1/5)
1. What is a goroutine in Go's concurrent execution model?
easy
A. A lightweight thread managed by the Go runtime
B. A type of variable used for synchronization
C. A special function that runs only once
D. A package for handling errors
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 A
Quick Check:
Goroutine = lightweight thread [OK]
Hint: Goroutine = lightweight thread in Go [OK]
Common Mistakes:
Confusing goroutine with OS thread
Thinking goroutine is a variable type
Assuming goroutine runs only once
2. Which of the following is the correct syntax to start a goroutine that calls function doWork()?
easy
A. go doWork()
B. goroutine doWork()
C. start doWork()
D. run doWork()
Solution
Step 1: Recall goroutine syntax
In Go, to start a goroutine, prefix the function call with the keyword go.
Step 2: Match syntax with options
Only go doWork() uses the correct keyword and syntax.
Final Answer:
go doWork() -> Option A
Quick Check:
Start goroutine with 'go' keyword [OK]
Hint: Use 'go' before function call to start goroutine [OK]
Common Mistakes:
Using 'goroutine' keyword instead of 'go'
Using 'start' or 'run' which are invalid
Omitting the 'go' keyword
3. What will be the output of the following Go program?
package main
import (
"fmt"
"time"
)
func main() {
go fmt.Println("Hello from goroutine")
fmt.Println("Hello from main")
time.Sleep(100 * time.Millisecond)
}
medium
A. No output
B. Hello from goroutine\nHello from main
C. Hello from main
D. Hello from main\nHello from goroutine
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
The time.Sleep allows the goroutine time to run before the program exits, so both lines print.
Final Answer:
Hello from main\nHello from goroutine -> Option D
Quick Check:
Sleep allows goroutine output after main [OK]
Hint: Sleep lets goroutine finish before program ends [OK]
Common Mistakes:
Ignoring need for sleep to let goroutine run
Assuming goroutine output always prints first
Expecting no output due to race condition
4. Identify the error in this Go code snippet that tries to run a goroutine:
package main
import "fmt"
func main() {
go func() {
fmt.Println("Running")
}()
}
medium
A. Missing import of time package
B. Program exits before goroutine runs
C. Syntax error in anonymous function
D. Cannot use go with anonymous functions
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 B
Quick Check:
Program ends before goroutine runs [OK]
Hint: Add sleep or sync to wait for goroutine [OK]
Common Mistakes:
Thinking syntax is wrong for anonymous goroutine
Forgetting program ends immediately
Assuming goroutine blocks main automatically
5. You want to run 3 goroutines that each print their number (1, 2, 3). Which code correctly prints all numbers reliably before the program exits?
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?
}
hard
A. wg.Done()
B. time.Sleep(1 * time.Second)
C. wg.Wait()
D. close(wg)
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
Calling wg.Wait() blocks main until all goroutines call Done().
Final Answer:
wg.Wait() -> Option C
Quick Check:
WaitGroup.Wait() waits for goroutines [OK]
Hint: Use wg.Wait() to wait all goroutines finish [OK]
Common Mistakes:
Using Sleep instead of WaitGroup for sync
Trying to close WaitGroup (not allowed)
Calling Done() in main instead of goroutine
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.