Quick Start
Install env, load a .env file, read typed values, and map them into a struct.
Installation
go get github.com/cybergodev/envRequirements
Go 1.25+
Create a .env File
Create a .env file in your project root:
# Database configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=secret
# Application configuration
DEBUG=true
APP_NAME=myapp
LOG_LEVEL=info
# Multiple values (comma-separated)
ALLOWED_HOSTS=localhost,example.com,api.example.comFull syntax reference
For the complete .env syntax rules (quoting, escapes, multi-line values), see File Format.
Minimal Usage
package main
import (
"fmt"
"github.com/cybergodev/env"
)
func main() {
// Load the .env file and apply to the system environment
if err := env.Load(".env"); err != nil {
panic(err)
}
// Get environment variables
host := env.GetString("DB_HOST", "localhost")
port := env.GetInt("DB_PORT", 5432)
fmt.Printf("Server: %s:%d\n", host, port)
// Output: Server: localhost:5432
}Two usage modes
env has two usage modes:
| Mode | Usage | Use Case |
|---|---|---|
| Global mode | env.Load() + env.GetString() | Simple applications, scripts, quick prototyping |
| Instance mode | env.New() + loader.GetString() | Multiple instances, test isolation, fine-grained lifecycle control |
Global mode uses package-level functions backed by a default Loader singleton. After env.Load() succeeds, every env.GetXxx() call reads from that instance. Instance mode creates an independent Loader via env.New(), which you use when you need isolation or several configurations at once.
The examples on this page use global mode. Multi-environment Configuration shows the instance-mode equivalent.
Calling package-level functions before Load() reports no error, but behavior differs: Get* returns the default or zero value, Lookup returns ("", false), Keys/All return nil and Len returns 0, while Set/Delete/Validate/ParseInto return ErrNotInitialized. See Package Functions for details.
Reading Values - All Types
Basic Types
// === With default values ===
// String - returns default "localhost" when not found
host := env.GetString("HOST", "localhost")
// Integer (int64) - returns default 8080 when not found
port := env.GetInt("PORT", 8080)
// Float (float64) - returns default 0.99 when not found
rate := env.GetFloat64("RATE", 0.99)
// Unsigned integer (uint64) - returns default 4096 when not found
size := env.GetUint64("MAX_SIZE", 4096)
// Boolean - returns default false when not found
debug := env.GetBool("DEBUG", false)
// Duration - returns default 30s when not found
timeout := env.GetDuration("TIMEOUT", 30*time.Second)// === Without default values ===
// String - returns empty string "" when not found
host := env.GetString("HOST")
// Integer (int64) - returns 0 when not found
port := env.GetInt("PORT")
// Boolean - returns false when not found
debug := env.GetBool("DEBUG")
// Duration - returns 0 when not found
timeout := env.GetDuration("TIMEOUT")Key-name resolution
A key lookup tries these forms in order:
// JSON: {"app": {"name": "myapp"}}
// Stored as: APP_NAME=myapp
// All of the following access this value (distinct names for illustration only)
nameFlat := env.GetString("APP_NAME") // Flat key (recommended)
nameDot := env.GetString("app.name") // Dot path (auto-converted)
nameUpper := env.GetString("APP.NAME") // Uppercase dot path
fmt.Println(nameFlat, nameDot, nameUpper) // all the sameResolution rules:
- Exact match: looks up the key as given (
KEY), including literal keys that contain dots (a YAML top-level keyapnews.comis stored in uppercase as APNEWS.COM) - Uppercase conversion: a lowercase key falls back to its uppercase form (
key→KEY) - Path resolution: dot paths convert to underscores (
app.name→APP_NAME) - Numeric index: a key ending in an index (
hosts.0) returns that element of the comma-separated value
Boolean Support
GetBool is case-insensitive: true/1/yes/on/enabled are truthy, and false/0/no/off/disabled are falsy.
An empty string parses as false rather than falling back to the default, and an unrecognized value is audited and returns the default. GetInt, GetFloat64, and GetDuration differ: their empty values fail to parse and fall back to the default. See FAQ · What boolean value formats are supported? for the full table.
Slice Types
// String slice
hosts := env.GetSlice[string]("HOSTS", []string{"localhost"})
// Integer slices (supports int, int64, uint, uint64)
ports := env.GetSlice[int64]("PORTS", []int64{80, 443})
portsInt := env.GetSlice[int]("PORTS") // also supports int
// Float slice
rates := env.GetSlice[float64]("RATES", []float64{0.1, 0.2})
// Boolean slice
flags := env.GetSlice[bool]("FLAGS", []bool{true, false})
// Duration slice
timeouts := env.GetSlice[time.Duration]("TIMEOUTS")Parse order:
- Look for indexed keys
KEY_0,KEY_1,KEY_2... - If none exist, split the
KEYvalue on commas
Indexed elements are parsed one by one; an element that fails conversion is recorded in the audit log and skipped. If nothing survives the skips, the key is treated as having no indexed keys and the comma-separated value is tried next; if that also fails to parse, the whole list is discarded and the caller's default is returned — no further candidate keys are tried.
// Method 1: Indexed keys (recommended)
// HOSTS_0=localhost
// HOSTS_1=example.com
hosts := env.GetSlice[string]("HOSTS") // ["localhost", "example.com"]
// Method 2: Comma-separated
// PORTS=80,443,8080
ports := env.GetSlice[int64]("PORTS") // [80, 443, 8080]Lookup and Inspection
// Check if a key exists
value, exists := env.Lookup("API_KEY")
if !exists {
// key does not exist
}
// Get all keys
keys := env.Keys()
// Get all key-value pairs
all := env.All()
// Get variable count
count := env.Len()Secure Values
secret := env.GetSecure("API_KEY")
if secret != nil {
defer secret.Release()
// Get the raw value (only call when plaintext is needed, e.g. for crypto, API calls)
value := secret.Reveal()
// Use mask for logging (prevent leakage); the number is the value length.
// A 32-byte API_KEY prints [SECURE:32 bytes] (a lock status is appended when
// memory locking is enabled).
log.Printf("API Key: %s", secret.Masked())
}Struct Mapping
Use struct tags to map environment variables to a struct:
package main
import (
"fmt"
"time"
"github.com/cybergodev/env"
)
type Config struct {
Host string `env:"DB_HOST" envDefault:"localhost"`
Port int64 `env:"DB_PORT" envDefault:"5432"`
Password string `env:"DB_PASSWORD"`
Debug bool `env:"DEBUG" envDefault:"false"`
Timeout time.Duration `env:"TIMEOUT" envDefault:"30s"`
Hosts []string `env:"ALLOWED_HOSTS"`
}
func main() {
if err := env.Load(".env"); err != nil {
panic(err)
}
var cfg Config
if err := env.ParseInto(&cfg); err != nil {
panic(err)
}
fmt.Printf("%+v\n", cfg)
// Output: {Host:localhost Port:5432 Password:secret Debug:true Timeout:30s Hosts:[localhost example.com api.example.com]}
}Details
See the Struct Mapping guide for tags, nested structs, and validation.
Configuration Presets
env ships four presets, one per common scenario:
| Preset | Purpose | Characteristics |
|---|---|---|
DefaultConfig() | General use | Safe defaults for most cases |
DevelopmentConfig() | Development | Relaxed limits, overwrites existing keys |
TestingConfig() | Testing | Small limits, overwrites existing keys, suited for unit tests |
ProductionConfig() | Production | Strict validation + audit logging |
// Development - relaxed limits
cfg := env.DevelopmentConfig()
// Testing - compact limits
cfg := env.TestingConfig()
// Production - strict validation + audit logging
cfg := env.ProductionConfig()Detailed Preset Comparison
| Feature | Default | Development | Testing | Production |
|---|---|---|---|---|
| Overwrite existing variables | ✗ | ✓ | ✓ | ✗ |
| Error on missing file | ✗ | ✗ | ✗ | ✓ |
| Audit logging | ✗ | ✗ | ✗ | ✓ |
| YAML syntax | ✗ | ✓ | ✗ | ✗ |
| File size limit | 8 MB | 10 MB | 64 KB | 64 KB |
| Max variables | 1000 | 1000 | 50 | 50 |
| Forbidden key check | ✓ | ✓ | ✓ | ✓ |
| Value validation | ✓ | ✓ | ✓ | ✓ |
How to choose
- Development:
DevelopmentConfig()for relaxed limits during fast iteration - Testing:
TestingConfig()for overwrite support and test isolation - Production:
ProductionConfig()for audit logging and strict validation
Multi-environment Configuration
Load by Environment
// Determine config file based on environment
goEnv := os.Getenv("GO_ENV")
if goEnv == "" {
goEnv = "development"
}
// Default: the first file loaded wins (keys already set by .env are
// not overridden by later files)
env.Load(".env", ".env."+goEnv, ".env.local")
// When you need last-file-wins (e.g. .env.local overrides .env):
cfg := env.DefaultConfig()
cfg.OverwriteExisting = true
cfg.Filenames = []string{".env", ".env." + goEnv, ".env.local"}
if err := env.LoadWithConfig(cfg); err != nil { log.Fatal(err) }Overwrite semantics
Later files do not override earlier ones by default: DefaultConfig() and ProductionConfig() both use first-file-wins and skip keys that already exist. This prevents a later file from silently replacing configuration loaded at runtime. Set OverwriteExisting = true (already set in the DevelopmentConfig()/TestingConfig() presets) for last-file-wins behavior.
Using a Loader Instance
When you need lifecycle control, create a Loader instance:
package main
import (
"fmt"
"github.com/cybergodev/env"
)
func main() {
// Create configuration (clear Filenames so New does not auto-load
// and validate the required keys itself)
cfg := env.ProductionConfig()
cfg.Filenames = nil
cfg.RequiredKeys = []string{"DB_HOST", "API_KEY"}
// Create a loader
loader, err := env.New(cfg)
if err != nil {
panic(err)
}
defer loader.Close()
// Load files (in order; under ProductionConfig the first file wins, existing keys are not overwritten)
if err := loader.LoadFiles(".env", ".env.production"); err != nil {
panic(err)
}
// Validate required keys (each file was already checked at parse time;
// this re-checks the merged store)
if err := loader.Validate(); err != nil {
panic(err)
}
// Use
host := loader.GetString("DB_HOST")
fmt.Println("Host:", host)
}Multi-file and Multi-format
Multi-file Loading
Files load in the order you list them. With DefaultConfig() the first file loaded wins: keys that already exist are skipped in later files. Set OverwriteExisting = true (already set in the DevelopmentConfig()/TestingConfig() presets) when later files should override earlier ones:
env.Load(".env", "config.json", "config.yaml")loader.LoadFiles(".env", ".env.local")Multi-format Support
LoadFiles picks the parser from the file extension:
loader.LoadFiles("config.env", "settings.json", "secrets.yaml")Supported formats
| Format | Extension | Detection |
|---|---|---|
| .env | .env | File extension |
| JSON | .json | File extension |
| YAML | .yaml, .yml | File extension |
Error Handling
import "errors"
err := loader.LoadFiles(".env")
if err != nil {
switch {
case errors.Is(err, env.ErrFileNotFound):
// file not found
case errors.Is(err, env.ErrFileTooLarge):
// file too large
case errors.Is(err, env.ErrSecurityViolation):
// forbidden key (actually returns *SecurityError)
default:
// other errors
}
// Invalid key format: actually returns *ValidationError, Field=="key"
var valErr *env.ValidationError
if errors.As(err, &valErr) && valErr.Field == "key" {
// invalid key format
}
}Getting detailed errors
// Parse error details
var parseErr *env.ParseError
if errors.As(err, &parseErr) {
fmt.Printf("File %s line %d: %v\n", parseErr.File, parseErr.Line, parseErr.Err)
}
// File error details
var fileErr *env.FileError
if errors.As(err, &fileErr) {
fmt.Printf("File %s operation %s failed: %v\n", fileErr.Path, fileErr.Op, fileErr.Err)
}
// Security error details
var secErr *env.SecurityError
if errors.As(err, &secErr) {
fmt.Printf("Security error: %s - %s\n", secErr.Action, secErr.Reason)
}Next Steps
Learn More
- Struct Mapping - configuration binding in detail
- Serialization - config serialization and deserialization
- Multi-format Config - JSON/YAML in detail
- Testing - using env in tests
API Reference
- Package Functions - every package-level function
- Loader API - Loader methods
- Config API - configuration options
Security
- Security Overview - security architecture and defaults
- SecureValue API - secure value handling