Reference / Config API for Go

Config API for Go

Every public type, function and constant of the Config engine in tinyshed.org/tinystore, generated from its source. The Config guide explains how to use them, and the Bun and Node, Python and Rust pages list the same API.

Config

go
type Config[T any] struct {
	// contains filtered or unexported fields
}

Config is a program's settings as one typed value, read from memory, which a change of a live setting made in any process replaces whole.

OpenConfig

go
func OpenConfig[T any](ctx context.Context, store *Store, name string, defaults T, opts ...ConfigOption) (*Config[T], error)

OpenConfig opens a config: defaults declare every setting, its type and its default, then each layer declares again what came before, in the order written, and a live setting's change, kept in the store, goes over them all. It opens once its layers are read, or fails with one Invalid error that lists every problem. The same open in the process shares one stream.

config, err := tinystore.OpenConfig(ctx, store, "app", Settings{Port: 8080},
	tinystore.File("app.yaml"), tinystore.Env("APP", tinystore.Var("port", "PORT")))

OpenConfigJSON

go
func OpenConfigJSON(
	ctx context.Context, store *Store, name string, declaration json.RawMessage, opts ...ConfigOption,
) (*Config[map[string]any], error)

OpenConfigJSON opens a config declared as data, its declaration the JSON every SDK sends: each setting with its kind, default, required, secret, live and rules, a group, a map of entries, or a group a text setting beside it chooses by name. Its value is a map whose secrets are Secret, durations time.Duration, integers int64 and numbers float64; layers, rules, secrets and Explain are as OpenConfig's. A declaration that means nothing is Invalid, naming where.

config, err := tinystore.OpenConfigJSON(ctx, store, "dashbin", declaration,
	tinystore.File("dashbin.toml"), tinystore.Env("DASHBIN"))
port := config.Get()["instance"].(map[string]any)["aimee"].(map[string]any)["settings"].(map[string]any)["port"]

Config.Explain

go
func (c *Config[T]) Explain() []Line

Explain is every setting with its value, the layer that gave it and what it went over, newest first, as text and in Given as data; Notes is what was read and not used.

Config.Get

go
func (c *Config[T]) Get() T

Get is the settings as they are now, a copy: read it where it is used.

Config.Notes

go
func (c *Config[T]) Notes() []string

Notes is what the config read and did not use, each with why: an optional file that was absent, a variable with the prefix that names no setting, a kept change left out, a value validate refused.

Config.Reset

go
func (c *Config[T]) Reset(ctx context.Context, path string) error

Reset drops the change kept of the setting or the group at path, so that the layers give it again; a path with no setting and no change is Invalid.

Config.ResetAll

go
func (c *Config[T]) ResetAll(ctx context.Context) error

ResetAll drops every change kept of the config.

Config.Update

go
func (c *Config[T]) Update(ctx context.Context, change func(*T), opts ...UpdateOption) error

Update changes live settings: change sets them on a copy of the value, and each setting it changed is kept in the store, over every layer until Reset, seen by every handle before Update returns, all of them or none. One that is not live is Invalid. A change raced by another runs change again on the newer value, unless IfVersion was given.

err = config.Update(ctx, func(s *Settings) { s.Limits.RPS = 200 })

Config.Version

go
func (c *Config[T]) Version() int64

Version is the version of the changes the value holds, which IfVersion takes.

Config.Watch

go
func (c *Config[T]) Watch(ctx context.Context) iter.Seq[T]

Watch is the settings now, then after every change made in this process or another, whole and once committed; one that falls behind gets the newest. It ends when ctx ends or the store closes.

for s := range config.Watch(ctx) {
	limiter.SetRate(s.Limits.RPS)
}

ConfigOption

go
type ConfigOption interface {
	// contains filtered or unexported methods
}

ConfigOption is a layer of a config after its declaration, or how it opens.

Env

go
func Env(prefix string, opts ...EnvOption) ConfigOption

Env reads the environment: prefix_ and each setting's path in upper snake case, APP_LIMITS_RPS for limits.rps, a list as a,b,c or JSON and a map as JSON. NAME_FILE reads the setting from that file, as Docker and Kubernetes pass secrets; a variable with the prefix that names no setting is said.

File

go
func File(path string, opts ...FileOption) ConfigOption

File is a TOML, YAML or JSON file the config reads, by its extension: .toml, .yaml or .yml, .json or .jsonc, JSON taking comments and a trailing comma. A relative path is the working directory's; one that does not exist is Invalid unless Optional.

Strict

go
func Strict() ConfigOption

Strict fails the open while a kept change no longer fits the declaration, rather than ignoring it: for a config whose defaults are worse than not starting.

Validate

go
func Validate[T any](check func(T) error) ConfigOption

Validate checks the whole value at open and before a change is kept: a change it refuses keeps nothing. What relates two settings, a format or a pattern, is its; T is the config's.

Entries

go
type Entries[T any] map[string]T

Entries is a map of named entries, each a T struct whose fields are settings of their own, with their own tags: a file gives an entry field by field, and APP_INSTANCE_AIMEE_PASSWORD, or its _FILE, one field of the entry aimee that a layer before it made. A field a layer leaves out is its zero value, as a list's item's is, and one tagged required is one every entry is given. A map is one setting, given whole.

EnvOption

go
type EnvOption func(*envLayer)

EnvOption is how a config reads the environment.

DotEnv

go
func DotEnv(files ...string) EnvOption

DotEnv reads these .env files for the variables the process lacks; one that does not exist is skipped.

EnvFrom

go
func EnvFrom(variables map[string]string) EnvOption

EnvFrom reads these variables in place of the process's, as a test gives them.

Var

go
func Var(path, name string) EnvOption

Var gives the setting at path a variable of its own in place of its prefixed one, such as the PORT and DATABASE_URL platforms set.

FileOption

go
type FileOption func(*fileLayer)

FileOption is how a config reads a file.

NoSecrets

go
func NoSecrets() FileOption

NoSecrets refuses the config when the file gives a secret, for a file committed beside the code, whose secrets come from the environment: the error names the variable and its _FILE that give it instead, never the secret, and a missing secret's error names no such file.

Optional

go
func Optional() FileOption

Optional lets the file be absent: then it declares nothing, and Explain says so.

Given

go
type Given struct {
	Value string
	From  Origin
}

Given is a value a setting was given, as JSON, a span spelled, a secret *** and none unset, and where it came from.

Line

go
type Line struct {
	Path, Value, From, Over string
	// Given is every value the setting was given, newest first: its own, then
	// the ones it went over; From is Given[0].From.String(). It is empty from
	// a server before 0.1.1.
	Given []Given
}

Line is a setting's line of Explain: its value as JSON, a span spelled, a secret *** and none unset, the layer that gave it and the values it went over, newest first.

Line.String

go
func (l Line) String() string

Origin

go
type Origin struct {
	Kind     OriginKind
	Place    int       // a layer's place in the list, the declaration 1
	Path     string    // a file's path
	Variable string    // an env's variable
	DotEnv   string    // the .env file that set the variable
	File     string    // the file a NAME_FILE variable named
	Version  int64     // a change's version
	At       time.Time // when a change was made
}

Origin is where a value came from: its kind, and only the fields that kind names, the others zero. Its String is the text a Line's From says: the declaration, layer 2, app.yaml, APP_PORT in .env, or a change, version 7, 2026-10-13 14:03:05 UTC. A kind from a newer server is kept as it was sent, and its String is the kind.

Origin.String

go
func (o Origin) String() string

String is the text a Line's From says, as the core writes it.

OriginKind

go
type OriginKind string

OriginKind is what gave a value.

OriginDeclaration

go
const (
	OriginDeclaration OriginKind = "declaration"
	OriginLayer       OriginKind = "layer" // an object in a Bun or Python program's list of layers
	OriginFile        OriginKind = "file"
	OriginEnv         OriginKind = "env"
	OriginChange      OriginKind = "change"
)

The kinds an origin has.

Secret

go
type Secret struct {
	// contains filtered or unexported fields
}

Secret is a setting's text that prints as *** wherever it goes: fmt, JSON, slog and errors. Reveal gives the text, where the program hands it to what needs it. Hiding it from print is not access control, which the server's tokens are.

NewSecret

go
func NewSecret(text string) Secret

NewSecret is a secret of this text, a declaration's default.

Secret.Format

go
func (Secret) Format(f fmt.State, _ rune)

Secret.GoString

go
func (Secret) GoString() string

Secret.LogValue

go
func (Secret) LogValue() slog.Value

Secret.MarshalJSON

go
func (Secret) MarshalJSON() ([]byte, error)

Secret.MarshalText

go
func (Secret) MarshalText() ([]byte, error)

Secret.Reveal

go
func (s Secret) Reveal() string

Reveal is the secret's text.

Secret.String

go
func (Secret) String() string

Secret.UnmarshalJSON

go
func (s *Secret) UnmarshalJSON(text []byte) error

UpdateOption

go
type UpdateOption func(*int64)

UpdateOption is what an update needs of the config.

IfVersion

go
func IfVersion(version int64) UpdateOption

IfVersion keeps the change only while the config is at this version: ErrConflict when another change came since, so that an admin page does not overwrite a change it never showed.

Generated from the source Updated 3 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results