Getting started

  1. Install
  2. Quick start
  3. Resolution priority
  4. Where to next

Install

go get github.com/bakhod1r/oneenv

Requires Go 1.26+.

Quick start

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/bakhod1r/oneenv"
)

type Config struct {
    Port    int           `env:"PORT" default:"8080"`
    Host    string        `env:"HOST,required"`
    Timeout time.Duration `env:"TIMEOUT" default:"5s"`
}

func main() {
    // Reads ".env" (if present) and merges it with the process environment.
    cfg, err := oneenv.Parse[Config]()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s:%d (timeout %s)\n", cfg.Host, cfg.Port, cfg.Timeout)
}
# .env
HOST=localhost
PORT=9090
TIMEOUT=30s

Resolution priority

For each field the value is resolved in this order:

  1. Explicit environment variable (via the Lookuper, default os.LookupEnv)
  2. .env file value
  3. default tag

With WithOverride(), .env file values take precedence over the process environment. A field with no value from any source and no default is left at its zero value — unless it is required.

Where to next