context

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 1 Imported by: 0

README

tinywasm/context

Project Badges

Minimalist context library for TinyGo.

Features

  • No Maps: Uses a fixed array of structs to avoid dynamic allocations.
  • Mutable: Supports in-place modification via Set(key, value).
  • Minimal Footprint: Designed to keep binary size at a minimum.
  • TinyGo Compatible: Fully compatible with TinyGo and WASM environments.
  • Fixed Capacity: Maximum of 16 key-value pairs (prioritizes latest values).

Installation

go get github.com/tinywasm/context

Usage

import "github.com/tinywasm/context"

// Create background context
ctx := context.Background()

// Add values (Keys and Values must be strings)
// In-place mutation (simpler for state machines):
_ = ctx.Set("user_id", "123")

// Nested creation (traditional style):
ctx, _ = context.WithValue(ctx, "role", "admin")

// Retrieve values
user := ctx.Value("user_id") // returns "123"
role := ctx.Value("role")    // returns "admin"
missing := ctx.Value("none") // returns ""

// Inspect keys
keys := ctx.Keys()           // returns []string{"user_id", "role"}

Constraints

  • Keys and Values are restricted to string type for performance and simplicity.
  • Maximum capacity of 16 key-value pairs. Exceeding this will return an error (see fmt error pattern).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context is a minimalist context compatible with TinyGo. No maps, no channels, uses a fixed array of 16 key-value pairs.

func Background added in v0.0.2

func Background() *Context

Background returns an empty Context (equivalent to context.Background).

func WithValue added in v0.0.2

func WithValue(parent *Context, key, value string) (*Context, error)

WithValue creates a new Context with the additional key-value pair. Returns errCapacityExceeded if the capacity of 16 pairs is exceeded.

func (*Context) Keys added in v0.0.10

func (c *Context) Keys() []string

Keys returns a slice containing all keys in the context. Later values with duplicate keys are not deduplicated - all keys are returned.

func (*Context) Set added in v0.0.9

func (c *Context) Set(key, value string) error

Set adds or updates a key-value pair in-place. Returns an error if the capacity of 16 pairs is exceeded.

func (*Context) Value added in v0.0.2

func (c *Context) Value(key string) string

Value searches for the value associated with key (reverse search to prioritize latest values).

Jump to

Keyboard shortcuts

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