env

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package env provides a small Reader abstraction over process environment access. Production code injects a Reader instead of calling os.Getenv directly so tests can substitute Map and avoid mutating the global process environment (which races under t.Parallel()).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(r Reader, name string) string

Get returns the value of name, or "" when unset. Convenience wrapper around Lookup matching the os.Getenv signature.

func GetBool

func GetBool(r Reader, name string, def bool) (bool, error)

GetBool parses common true/false spellings (1/0, true/false, yes/no, on/off — case-insensitive). Returns def when unset or empty. Returns an error when set but unparseable.

func GetInt

func GetInt(r Reader, name string, def int) (int, error)

GetInt parses name as a signed decimal integer. Returns def when name is unset or empty. Returns an error when set but unparseable.

func GetTrim

func GetTrim(r Reader, name string) string

GetTrim returns the value of name with leading/trailing whitespace removed. Returns "" when unset.

Types

type Map

type Map map[string]string

Map is a Reader backed by an in-memory map. The zero value is usable and reports every variable as unset.

Map is safe to use concurrently for reads; tests typically populate it once before injecting it.

func (Map) Lookup

func (m Map) Lookup(name string) (string, bool)

Lookup implements Reader.

func (Map) With

func (m Map) With(name, value string) Map

With returns a new Map with name set to value, leaving the receiver untouched. Useful for chaining setup.

type OS

type OS struct{}

OS is a Reader backed by os.LookupEnv.

func (OS) Lookup

func (OS) Lookup(name string) (string, bool)

Lookup implements Reader using os.LookupEnv.

type Reader

type Reader interface {
	// Lookup returns the value of name and whether it was set. An
	// empty string with ok==true means "set to empty"; an empty
	// string with ok==false means "unset".
	Lookup(name string) (value string, ok bool)
}

Reader reads environment-variable-style configuration.

var Default Reader = OS{}

Default is a process-wide OS reader for callers that cannot reach a composition root. Prefer injecting a Reader explicitly.

Jump to

Keyboard shortcuts

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