- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-best-practices/context-first-argument
Language: Go
Severity: Warning
Category: Best Practices
In Go, it is a best practice to use the context
package and pass a context.Context
as the first argument to functions that are expected to be long-running, have potential timeouts, or require cancellation.
Here are the reasons why using context
as the first argument is recommended:
context
package provides functionality to handle cancellation and timeouts in a clean and controlled way. By passing a context.Context
as the first argument, you allow callers of the function to control the lifecycle of the function execution by canceling it or setting timeouts. This promotes better resource management and avoids situations where long-running operations are left unfinished or hang indefinitely.context.Context
as the first argument, you create a clear and consistent pattern for propagating the context throughout your codebase. This allows the context to flow seamlessly across different function calls, enabling features like cancellation and timeout to be propagated correctly to the lower levels of your program.context
as the first argument makes it easier to write unit tests for your functions. You can create a context.Context
object with specific cancellation or timeouts in your tests and validate the behavior of your function under different scenarios. This enables better test coverage and ensures that your functions respond correctly to different context states.context.Context
, you improve the readability of your code. Developers who read your code can immediately understand the intent and requirements of the function just by looking at the function signature. This makes it easier to reason about the behavior of the function in different context scenarios.By following the convention of using context
as the first argument, you adhere to Go’s idiomatic practices, improve code readability, facilitate testability, and allow for better control and propagation of cancellation and timeouts. This helps in building robust, maintainable, and scalable Go codebases.
package main
import (
"context"
)
func main(){
serve(0, context.TODO())
}
func serve(port int, ctx context.Context){
fmt.Println(port)
fmt.Printf("%+v\n", ctx)
}
func serve(port int, blah string, ctx context.Context){
fmt.Println(port)
fmt.Printf("%+v\n", ctx)
}
func anotherServe(ctx context.Context, myVar,anotherVar string){
return
}
package main
import (
"context"
)
func main(){
serve(0, context.TODO())
}
func serve(ctx context.Context, port int){
fmt.Println(port)
fmt.Printf("%+v\n", ctx)
}
func serve(ctx context.Context, port int, blah string){
fmt.Println(port)
fmt.Printf("%+v\n", ctx)
}
func anotherServe(ctx context.Context, myVar,anotherVar string){
return
}
func displayHelpMessage(ctx workflow.Context, args interface{}, action *something.Action, sb *strings.Builder, executionContext something.ExecutionContext) error {
}
func testAgainWithTesting(t *testing.T, ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer, args []string) error {
}
package main
import (
"context"
)
func main(){
serve(0, context.TODO())
}
func serve(ctx context.Context, port int){
fmt.Println(port)
fmt.Printf("%+v\n", ctx)
}
func serve(ctx context.Context, port int, blah string){
fmt.Println(port)
fmt.Printf("%+v\n", ctx)
}
func anotherServe(ctx context.Context, myVar,anotherVar string){
return
}
func displayHelpMessage(ctx workflow.Context, args interface{}, action *something.Action, sb *strings.Builder, executionContext something.ExecutionContext) error {
}