Go

Context Package

context.Context, cancellation, timeouts, deadlines, values, WithCancel, WithTimeout

20 면접 질문·
Mid-Level
1

What is the main role of the context package in Go?

답변

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.

2

Which function creates an empty context without deadline or cancellation?

답변

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.

3

What is the difference between context.Background() and context.TODO()?

답변

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.

4

How to create a context that can be manually cancelled?

5

Which channel allows detecting the cancellation of a context?

+17 면접 질문

다음 면접을 위해 Go을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기