context

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: MIT Imports: 4 Imported by: 0

README

context Package

The context package extends Go's standard context.Context with advanced configuration and management features. It provides generic, thread-safe context storage, context cloning, merging, and key-value management, making it easier to handle complex application state and configuration.

Features

  • Generic context configuration with type-safe keys
  • Thread-safe key-value storage and retrieval
  • Context cloning and merging
  • Walk and filter stored values
  • Integration with Go's context.Context interface

Main Types & Functions

  • Config[T comparable]: Generic interface for context configuration and key-value management.
  • MapManage[T]: Interface for map operations (load, store, delete, clean).
  • FuncContext: Function type returning a context.Context.
  • NewConfig: Create a new generic context configuration.
Example Usage
import "github.com/nabbar/golib/context"

type MyKey string

cfg := context.NewConfig[MyKey](nil)
cfg.Store("myKey", "myValue")
val, ok := cfg.Load("myKey")
Key Methods
  • SetContext(ctx FuncContext)
  • Clone(ctx context.Context) Config[T]
  • Merge(cfg Config[T]) bool
  • Walk(fct FuncWalk[T]) bool
  • LoadOrStore(key T, cfg interface{}) (val interface{}, loaded bool)
  • Clean()
IsolateParent

Creates a new context with cancelation, isolated from the parent.

ctx := context.IsolateParent(parentCtx)

context/gin Subpackage

The context/gin subpackage provides a bridge between Go's context and the Gin web framework. It wraps Gin's Context to add context management, signal handling, and metadata utilities for HTTP request handling.

Features

  • Wraps Gin's Context with Go's context.Context
  • Signal-based cancellation (e.g., on OS signals)
  • Key-value storage and retrieval for request-scoped data
  • Type-safe getters for common types (string, int, bool, etc.)
  • Integrated logging support

Main Types & Functions

  • GinTonic: Interface combining context.Context and Gin's Context with extra helpers.
  • New: Create a new GinTonic context from a Gin Context and logger.
Example Usage
import (
    "github.com/nabbar/golib/context/gin"
    "github.com/gin-gonic/gin"
    "github.com/nabbar/golib/logger"
)

func handler(c *gin.Context) {
    ctx := gin.New(c, logger.New(nil))
    ctx.Set("userID", 123)
    id := ctx.GetInt("userID")
    // ...
}
Key Methods
  • Set(key string, value interface{})
  • Get(key string) (value interface{}, exists bool)
  • GetString(key string) string
  • CancelOnSignal(s ...os.Signal)
  • SetLogger(log liblog.FuncLog)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsolateParent

func IsolateParent(parent context.Context) context.Context

Types

type Config

type Config[T comparable] interface {
	context.Context
	MapManage[T]
	Context

	SetContext(ctx FuncContext)
	Clone(ctx context.Context) Config[T]
	Merge(cfg Config[T]) bool
	Walk(fct FuncWalk[T]) bool
	WalkLimit(fct FuncWalk[T], validKeys ...T) bool

	LoadOrStore(key T, cfg interface{}) (val interface{}, loaded bool)
	LoadAndDelete(key T) (val interface{}, loaded bool)
}

func NewConfig

func NewConfig[T comparable](ctx FuncContext) Config[T]

type Context added in v1.10.0

type Context interface {
	GetContext() context.Context
}

type FuncContext added in v1.10.0

type FuncContext func() context.Context

type FuncContextConfig added in v1.10.0

type FuncContextConfig[T comparable] func() Config[T]

type FuncWalk added in v1.10.0

type FuncWalk[T comparable] func(key T, val interface{}) bool

type MapManage added in v1.10.0

type MapManage[T comparable] interface {
	Clean()
	Load(key T) (val interface{}, ok bool)
	Store(key T, cfg interface{})
	Delete(key T)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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