context

Concurrency
import "context"

Provides request-scoped values, cancellation signals, and deadlines across API boundaries and goroutines.

Example

package main

import (
    "context"
    "fmt"
    "time"
)

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    fmt.Println(ctx.Deadline())
}

Key Types & Functions

ContextBackgroundTODOWithCancelWithTimeoutWithValue

About context

The context package (imported as context) belongs to the Concurrency category of Go packages. Provides request-scoped values, cancellation signals, and deadlines across API boundaries and goroutines.

Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The context package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.

When using context in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.

Related Packages