example_context

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 5 Imported by: 0

README

Context Package Examples

This directory contains examples demonstrating how to use the context package, which provides utilities for working with Go's standard context package. The package offers enhanced functionality for context creation, value propagation, timeout management, and error handling.

Examples

1. Basic Usage Example

basic_usage_example.go

Demonstrates basic usage of the context package.

Key concepts:

  • Creating contexts with default and custom options
  • Automatically generating request IDs, trace IDs, and correlation IDs
  • Retrieving context information
  • Using Background and TODO contexts
2. Timeout Example

timeout_example.go

Shows how to use different timeout functions provided by the context package.

Key concepts:

  • Using predefined timeout durations (default, short, long)
  • Creating specialized timeout contexts (database, network, external service)
  • Setting custom timeouts
  • Handling context deadline exceeded errors
3. Value Propagation Example

value_propagation_example.go

Demonstrates how to add and retrieve values from contexts.

Key concepts:

  • Adding individual values to contexts (user ID, tenant ID, operation, etc.)
  • Adding multiple values at once
  • Propagating context values through function calls
  • Using WithValues for arbitrary key-value pairs
  • Automatic generation of IDs
4. Error Handling Example

error_handling_example.go

Shows how to handle context-related errors.

Key concepts:

  • Handling context timeout errors
  • Handling context cancellation
  • Using CheckContext to check if a context is done
  • Using MustCheck for critical operations
  • Error context with operation and service name

Running the Examples

To run any of the examples, use the go run command:

go run examples/context/basic_usage_example.go

Key Features of the Context Package

  1. Enhanced Context Creation: Create contexts with various options like timeout, request ID, trace ID, user ID, tenant ID, operation name, correlation ID, and service name.

  2. Predefined Timeouts: Use predefined timeout durations for common scenarios:

    • Default Timeout: 30 seconds
    • Short Timeout: 5 seconds
    • Long Timeout: 60 seconds
    • Database Timeout: 10 seconds
    • Network Timeout: 15 seconds
    • External Service Timeout: 20 seconds
  3. Value Management: Add and retrieve values from contexts with type-safe accessors.

  4. Error Handling: Check context status and handle errors with detailed information.

  5. Automatic ID Generation: Automatically generate request IDs, trace IDs, and correlation IDs when not provided.

Additional Resources

For more information about the context package, see the context package documentation.

Documentation

Overview

Example of basic usage of the context package

Example of error handling with the context package

Example of using timeouts with the context package

Example of value propagation with the context package

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL