
Context Package
context.Context, cancellation, timeouts, deadlines, values, WithCancel, WithTimeout
1What is the main role of the context package in Go?
What is the main role of the context package in Go?
Resposta
The context package allows managing timeouts, cancellations and deadlines consistently across the entire call chain. This prevents zombie goroutines that continue running when their result is no longer needed. Always pass the context as the first parameter and regularly check Done() in long operations to respect cancellations.
2Which function creates an empty context without deadline or cancellation?
Which function creates an empty context without deadline or cancellation?
Resposta
context.Background() returns an empty, non-cancellable context without deadline or values. It serves as a starting point for creating derived contexts and is typically used in main, initialization or tests. Never use nil as a context as it causes panics when accessing methods.
3What is the difference between context.Background() and context.TODO()?
What is the difference between context.Background() and context.TODO()?
Resposta
context.TODO() serves as a placeholder when the appropriate context is not yet clear or available during development. context.Background() is used for real entry points like main or HTTP handlers. Both are functionally identical but TODO() documents a temporary intention that should be replaced later.
How to create a context that can be manually cancelled?
Which channel allows detecting the cancellation of a context?
+17 perguntas de entrevista
Outros temas de entrevista Go
Go Basics
Go Data Structures
Go Interfaces
Error Handling
Goroutines Basics
Channels
Go Modules
HTTP Server
HTTP Client
JSON Encoding
database/sql
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
Domine Go para sua proxima entrevista
Acesse todas as perguntas, flashcards, testes tecnicos, exercicios de code review e simuladores de entrevista.
Comece gratis