envconfig

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 6 Imported by: 0

README

go-envconfig

Lightweight environment variable config loader for Go. Populates structs from env vars using struct tags.

Install

go get github.com/BufferZoneCorp/go-envconfig@v0.1.0

Usage

import "github.com/BufferZoneCorp/go-envconfig"

type Config struct {
    Host    string   `env:"HOST"    default:"localhost"`
    Port    int      `env:"PORT"    default:"8080"`
    Debug   bool     `env:"DEBUG"   default:"false"`
    Tags    []string `env:"TAGS"`
}

var cfg Config
if err := envconfig.Load(&cfg); err != nil {
    log.Fatal(err)
}

Environment variables

Var Effect
ENVCONFIG_PREFIX Prepend a namespace to all lookups (e.g. MYAPP → looks up MYAPP_HOST)
ENVCONFIG_DEBUG=1 Print each key resolution to stderr

Supported field types

string, bool, int/int64, uint/uint64, float64, []string (comma-separated)

License

MIT

Documentation

Overview

Package envconfig populates Go structs from environment variables. Field names are mapped to env var names via the `env` struct tag.

Example:

type Config struct {
    Host string `env:"HOST" default:"localhost"`
    Port int    `env:"PORT" default:"8080"`
}
var cfg Config
if err := envconfig.Load(&cfg); err != nil { ... }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key, defaultVal string) string

Get returns the value of an env var or defaultVal if absent.

func Load

func Load(dst interface{}) error

Load reads environment variables into dst, which must be a pointer to a struct. Each exported field is mapped via its `env` tag; if absent the field name is upper-cased and used directly. The `default` tag supplies a fallback value.

func Lookup

func Lookup(key string) (string, bool)

Lookup returns the value of an env var, applying the global prefix if set. Returns ("", false) when the var is absent.

func MustLoad

func MustLoad(dst interface{})

MustLoad is like Load but panics on error.

func SetPrefix

func SetPrefix(p string)

SetPrefix overrides the global prefix at runtime (useful in tests).

Types

This section is empty.

Jump to

Keyboard shortcuts

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