env

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 6 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package env provides typed accessors for environment variables with fallback defaults.

Get* functions return the parsed value or a fallback; MustGet* functions panic when the variable is not set. Every accessed key is recorded with its type and default, which can be inspected through RequiredKeys, Defaults, Types and TypeOf. All functions are safe for concurrent use.

port := env.GetInt("PORT", 8080)
token := env.MustGet("API_TOKEN")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Defaults added in v0.17.0

func Defaults() map[string]any

Defaults returns the recorded default values by key.

func Exists

func Exists(key string) bool

Exists reports whether the environment variable key is set, even if empty.

func Get

func Get(key, fallback string) string

Get returns the value of the environment variable key, or fallback if it is not set. The first fallback seen for key is recorded as its default.

func GetBool

func GetBool(key string, fallback bool) bool

GetBool returns the environment variable key parsed with strconv.ParseBool, or fallback if it is not set or cannot be parsed.

func GetFloat64 added in v0.10.21

func GetFloat64(key string, fallback float64) float64

GetFloat64 returns the environment variable key parsed as float64, or fallback if it is not set or cannot be parsed.

func GetInt

func GetInt(key string, fallback int) int

GetInt returns the environment variable key parsed as int, or fallback if it is not set or cannot be parsed.

func GetInt64 added in v0.10.21

func GetInt64(key string, fallback int64) int64

GetInt64 returns the environment variable key parsed as base-10 int64, or fallback if it is not set or cannot be parsed.

func GetIntSlice

func GetIntSlice(key string, fallback []int) []int

GetIntSlice returns the environment variable key split on "," and parsed as ints, or fallback if it is unset, empty or any element cannot be parsed.

func GetStringSlice

func GetStringSlice(key string, fallback []string) []string

GetStringSlice returns the environment variable key split on ",", or fallback if it is unset or empty. Elements are not trimmed.

func MustExists

func MustExists(key string)

MustExists panics if the environment variable key is not set and otherwise records key as required.

func MustGet

func MustGet(key string) string

MustGet returns the value of the environment variable key and panics if it is not set.

func MustGetBool

func MustGetBool(key string) bool

MustGetBool returns the environment variable key parsed as bool and panics if it is not set. An unparsable value yields false.

func MustGetFloat64 added in v0.10.21

func MustGetFloat64(key string) float64

MustGetFloat64 returns the environment variable key parsed as float64 and panics if it is not set. An unparsable value yields 0.

func MustGetGetIntSlice

func MustGetGetIntSlice(key string) []int

MustGetGetIntSlice returns the environment variable key as []int (see GetIntSlice) and panics if it is not set.

func MustGetInt

func MustGetInt(key string) int

MustGetInt returns the environment variable key parsed as int and panics if it is not set. An unparsable value yields 0.

func MustGetInt64 added in v0.10.21

func MustGetInt64(key string) int64

MustGetInt64 returns the environment variable key parsed as int64 and panics if it is not set. An unparsable value yields 0.

func MustGetStringSlice

func MustGetStringSlice(key string) []string

MustGetStringSlice returns the environment variable key split on "," and panics if it is not set.

func Readme deprecated added in v0.17.0

func Readme() string

Readme returns a markdown table of all accessed environment variables.

Deprecated: Readme will be removed in a future release.

func RequiredKeys added in v0.17.0

func RequiredKeys() []string

RequiredKeys returns all keys recorded as required, in unspecified order.

func TypeOf added in v0.17.0

func TypeOf(key string) string

TypeOf returns the recorded type name of key, or "" if key is unknown.

func Types added in v0.17.0

func Types() map[string]string

Types returns the recorded type names by key.

Types

This section is empty.

Jump to

Keyboard shortcuts

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