config

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Overview

Package config provides a thin wrapper around cleanenv for loading application configuration from environment variables.

Define a config struct with env tags and call Load:

type Config struct {
    Port     int    `env:"PORT"     env-default:"8080"`
    DSN      string `env:"DATABASE_URL" env-required:"true"`
    LogLevel string `env:"LOG_LEVEL"    env-default:"info"`
}

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

Package config provides environment-based configuration loading.

Define a config struct with env tags:

type Config struct {
    Port        int    `env:"PORT"         env-default:"8080"`
    DatabaseURL string `env:"DATABASE_URL" env-required:"true"`
    LogLevel    string `env:"LOG_LEVEL"    env-default:"info"`
}

Load from environment:

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

Load from a .env file (variables in environment take precedence):

if err := config.LoadFile(".env", &cfg); err != nil {
    log.Fatal(err)
}

Supported tag options:

  • env:"VAR_NAME" — environment variable name
  • env-default:"value" — default value when variable is not set
  • env-required:"true" — return error when variable is not set

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(cfg any) error

Load reads environment variables into the provided struct pointer. Fields are mapped via `env:"VAR_NAME"` tags. Use `env-default:"value"` for defaults, `env-required:"true"` for required fields.

func LoadFile

func LoadFile(path string, cfg any) error

LoadFile reads a .env file and then env variables into the provided struct. Variables set in the environment take precedence over the file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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