env

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package env has simple functions for environment variables manipulation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v0.2.0

func As[T any](envKey string, parser func(string) (T, error)) (T, error)

As returns the value of the environment variable envKey parsed as T using parser. Returns an error if the variable is not set (eq "") or parser returns an error. parser must be a function of the form func(string) (T, error).

func AsDefault added in v0.2.0

func AsDefault[T any](envKey string, defaultVal T, parser func(string) (T, error)) (T, error)

AsDefault returns the value of the environment variable envKey parsed as T using parser. If the variable is not set (eq ""), it returns defaultVal and a nil error. Returns an error if parser returns an error for the raw string value. parser must be a function of the form func(string) (T, error).

func Bool added in v0.2.0

func Bool(envKey string) (bool, error)

Bool returns the value of the environment variable envKey as a bool. Returns an error if the variable is not set (eq "") or cannot be parsed as a bool. Parsing is delegated to strconv.ParseBool.

func BoolDefault added in v0.2.0

func BoolDefault(envKey string, defaultVal bool) (bool, error)

BoolDefault returns the value of the environment variable envKey as a bool. If the variable is not set (eq ""), it returns defaultVal and a nil error. Parsing is delegated to strconv.ParseBool, which accepts: 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Returns an error for any other value.

func Duration added in v0.2.0

func Duration(envKey string) (time.Duration, error)

Duration returns the value of the environment variable envKey as a time.Duration. Returns an error if the variable is not set (eq "") or cannot be parsed as a duration. Parsing is delegated to time.ParseDuration. Valid units: ns, us (or µs), ms, s, m, h.

func DurationDefault added in v0.2.0

func DurationDefault(envKey string, defaultVal time.Duration) (time.Duration, error)

DurationDefault returns the value of the environment variable envKey as a time.Duration. If the variable is not set (eq ""), it returns defaultVal and a nil error. Parsing is delegated to time.ParseDuration. Valid units: ns, us (or µs), ms, s, m, h. Returns an error if the value cannot be parsed as a duration.

func EnvAsBool deprecated

func EnvAsBool(envKey string, defaultVal bool) bool

Deprecated: use BoolDefault instead. Will be removed in v1.0.0 EnvAsBool returns the envKey as bool or the default value if missing (eq "") Panics if the value of the envKey is NOT a bool (parsing with strconv.ParseBool)

func EnvAsInt deprecated

func EnvAsInt(envKey string, defaultVal int) int

Deprecated: use IntDefault instead. Will be removed in v1.0.0 EnvAsInt returns the envKey as int or the default value if missing (eq "") Panics if the value of the envKey is NOT an integer

func EnvOrDefault deprecated

func EnvOrDefault(envKey, defaultVal string) string

Deprecated: use StringDefault instead. Will be removed in v1.0.0 EnvOrDefault will return the value of the environment variable key (envKey), or if not found (eq "") returns the default (defaultVal)

func Int added in v0.2.0

func Int(envKey string) (int, error)

Int returns the value of the environment variable envKey as an int. Returns an error if the variable is not set (eq "") or cannot be parsed as an integer.

func IntDefault added in v0.2.0

func IntDefault(envKey string, defaultVal int) (int, error)

IntDefault returns the value of the environment variable envKey as an int. If the variable is not set (eq ""), it returns defaultVal and a nil error. Returns an error if the value cannot be parsed as an integer.

func MustAs added in v0.2.0

func MustAs[T any](envKey string, parser func(string) (T, error)) T

MustAs returns the value of the environment variable envKey parsed as T using parser. Panics if the variable is not set (eq "") or parser returns an error. parser must be a function of the form func(string) (T, error).

func MustBool added in v0.2.0

func MustBool(envKey string) bool

MustBool returns the value of the environment variable envKey as a bool. Panics if the variable is not set (eq "") or cannot be parsed as a bool. Parsing is delegated to strconv.ParseBool.

func MustDuration added in v0.2.0

func MustDuration(envKey string) time.Duration

MustDuration returns the value of the environment variable envKey as a time.Duration. Panics if the variable is not set (eq "") or cannot be parsed as a duration. Parsing is delegated to time.ParseDuration. Valid units: ns, us (or µs), ms, s, m, h.

func MustEnv deprecated

func MustEnv(envKey string) string

Deprecated: use String or MustString instead. Will be removed in v1.0.0 MustEnv will return the value of the environment variable key (envKey) or panic if this is not found (eq "")

func MustEnvAsBool deprecated

func MustEnvAsBool(envKey string) bool

Deprecated: use Bool or MustBool instead. Will be removed in v1.0.0 MustEnvAsBool returns the envKey as bool Panics if envKey is NOT set Panics if the value of the envKey is NOT a bool (parsing with strconv.ParseBool)

func MustEnvAsInt deprecated

func MustEnvAsInt(envKey string) int

Deprecated: use Int or MustInt instead. Will be removed in v1.0.0 MustEnvAsInt returns the envKey as int or panics if missing (eq "") Panics if the value of the envKey is NOT an integer

func MustInt added in v0.2.0

func MustInt(envKey string) int

MustInt returns the value of the environment variable envKey as an int. Panics if the variable is not set (eq "") or cannot be parsed as an integer.

func MustString added in v0.2.0

func MustString(envKey string) string

MustString returns the value of the environment variable envKey as a string. Panics if the variable is not set (eq "").

func MustStringSplice added in v0.2.0

func MustStringSplice(envKey string, sep string) []string

MustStringSplice returns the value of the environment variable envKey split into a []string using sep as the delimiter. Panics if the variable is not set (eq ""). No trimming or filtering of elements is applied; the raw strings.Split result is returned.

func String added in v0.2.0

func String(envKey string) (string, error)

String returns the value of the environment variable envKey as a string. Returns an error if the variable is not set (eq "").

func StringDefault added in v0.2.0

func StringDefault(envKey string, defaultVal string) (string, error)

StringDefault returns the value of the environment variable envKey as a string. If the variable is not set (eq ""), it returns defaultVal and a nil error. A non-empty value is always returned without error regardless of its content.

func StringSplice added in v0.2.0

func StringSplice(envKey string, sep string) ([]string, error)

StringSplice returns the value of the environment variable envKey split into a []string using sep as the delimiter. Returns an error if the variable is not set (eq ""). No trimming or filtering of elements is applied; the raw strings.Split result is returned.

func StringSpliceDefault added in v0.2.0

func StringSpliceDefault(envKey string, sep string, defaultVal []string) ([]string, error)

StringSpliceDefault returns the value of the environment variable envKey split into a []string using sep as the delimiter. If the variable is not set (eq ""), it returns defaultVal and a nil error. No trimming or filtering of elements is applied; the raw strings.Split result is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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