env

package module
v0.0.0-...-99694e5 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2025 License: Apache-2.0 Imports: 8 Imported by: 1

README

env

Use environment variables as a struct.

type XDG struct {
    CacheHome  string
    State      string `env:"STATE_HOME"`
    RuntimeDir string
    Data       struct {
        Home string
        Dirs []string `env:,split=:"`
    }
    Config struct {
        Home string `env:",required"`
        Dirs []string `env:",split=:"`
    }
}

func main() {
    var xdg XDG
    err := env.ReadEnvPrefixed("xdg", &xdg)
    if err != nil {
        log.Fatal(err)
    }
    if xdg.Data.Home != os.Getenv("XDG_DATA_HOME") {
        log.Fatal("this should be the same")
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequired = errors.New("environment variable is required")
)

Functions

func Get

func Get(key string, defaultValue ...string) string

func ReadEnv

func ReadEnv(dst any) error

ReadEnv will populate the type with the appropriate environment variables.

func ReadEnvPrefixed

func ReadEnvPrefixed(prefix string, dst any) error

ReadEnvPrefixed is like ReadEnv except will use an environment variable prefix.

Types

This section is empty.

Jump to

Keyboard shortcuts

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