env

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AppEnvironmentKey is the key for the app environment, i.e. prod/dev
	AppEnvironmentKey = "APP_ENV"
)

Common keys

Variables

All defines all environments combined.

Functions

func Get

func Get(key, fallback string) string

Get provides a default override for environment vars as a second argument.

func GetOrError

func GetOrError(key string) (string, error)

GetOrError will attempt to get the value from the environment variable and return an error if not available.

func IsDevelopment

func IsDevelopment() bool

IsDevelopment returns whether we are running the app in development.

func IsProduction

func IsProduction() bool

IsProduction returns whether we are running the app in production.

func IsStaging

func IsStaging() bool

IsStaging returns whether we are running the app in staging.

func ParseConfig

func ParseConfig(cfg any, filenames ...string) error

ParseConfig loads the environment variables from the .env file and parses the environment variables into the provided struct. It returns an error if the .env file cannot be loaded or if the environment variables cannot be parsed.

For example:

type Config struct {
	Home         string         `env:"HOME"`
	Port         int            `env:"PORT" envDefault:"3000"`
	Password     string         `env:"PASSWORD,unset"`
	IsProduction bool           `env:"PRODUCTION"`
	Duration     time.Duration  `env:"DURATION"`
	Hosts        []string       `env:"HOSTS" envSeparator:":"`
	TempFolder   string         `env:"TEMP_FOLDER,expand" envDefault:"${HOME}/tmp"`
	StringInts   map[string]int `env:"MAP_STRING_INT"`
}

Types

type Environment

type Environment string

Environment represents the type of env.

const (
	// Development env definition.
	Development Environment = "development"
	// Staging env definition.
	Staging Environment = "staging"
	// Production env definition.
	Production Environment = "production"
)

Environment definitions.

func AppEnvironment

func AppEnvironment() Environment

AppEnvironment returns the app environment.

func (Environment) Short

func (e Environment) Short() string

Short returns a short name for the environment.

func (Environment) String

func (e Environment) String() string

String implements fmt.Stringer on Command.

Jump to

Keyboard shortcuts

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