
Goroutines Basics
Goroutines, go keyword, sync.WaitGroup, runtime.GOMAXPROCS, goroutine leaks
1What is a goroutine in Go?
What is a goroutine in Go?
Answer
A goroutine is a function that executes concurrently with other functions, managed by the Go runtime rather than the OS. It is extremely lightweight (a few KB of initial stack) compared to an OS thread (several MB). The Go scheduler can multiplex thousands or even millions of goroutines onto a limited number of OS threads, making concurrency very accessible in Go.
2How to start a new goroutine in Go?
How to start a new goroutine in Go?
Answer
The go keyword followed by a function call starts a new goroutine. For example, 'go myFunction()' or 'go func() { ... }()' for an anonymous function. The goroutine executes immediately in parallel with the calling code. Warning: the main function does not automatically wait for launched goroutines to finish.
3What is the main difference between a goroutine and an OS thread?
What is the main difference between a goroutine and an OS thread?
Answer
A goroutine consumes about 2 KB of initial stack while an OS thread typically requires 1-2 MB. The Go runtime manages goroutine scheduling in user space (M:N scheduling), multiplexing thousands of goroutines onto a few OS threads. This allows creating millions of goroutines without overloading the system, unlike OS threads which are limited by resources.
What happens if the main program terminates before a goroutine finishes?
How does the Go runtime manage goroutine execution?
+17 interview questions
Other Go interview topics
Go Basics
Go Data Structures
Go Interfaces
Error Handling
Channels
Go Modules
HTTP Server
HTTP Client
JSON Encoding
database/sql
Context Package
Testing
Concurrency Patterns
Sync Primitives
Go Web Frameworks
REST API Design
gRPC
Reflection
Memory Management
Performance Optimization
Generics
Go Design Patterns
Microservices
Security & Authentication
Docker & Containerization
Kubernetes Basics
Advanced Go
CLI Development
Master Go for your next interview
Access all questions, flashcards, technical tests, code review exercises and interview simulators.
Start for free