0
0
Goprogramming~5 mins

Goroutine creation - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aasync
Bgo
Cthread
Dspawn
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
Which is the safest way for goroutines to communicate?
AUsing files
BGlobal variables without locks
CPrinting to console
DChannels
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
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
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.