Documentation
ΒΆ
Overview ΒΆ
Package env provides a powerful, modern environment management library for Go.
Features:
- Basic Operations (basic.go): Get, Set, Unset, Lookup environment variables
- Base64 Support (base64.go): Automatic encoding/decoding of base64 values
- Sealed Secrets (sealed.go): AES-GCM encryption/decryption for sensitive data
- File Operations (files.go): Load from and save to .env files
- Struct Binding (binding.go): Bind environment variables to struct fields with type conversion
Example usage:
// Basic operations
value := env.Get("DATABASE_URL", "default_value")
env.Set("API_KEY", "secret123")
// Base64 operations
encoded, _ := env.GetB64("CONFIG_DATA")
env.SetB64("BINARY_DATA", "binary content")
// Sealed secrets
secret, _ := env.GetSealed("ENCRYPTED_PASSWORD", "MASTER_KEY")
env.SetSealed("CREDIT_CARD", "4111-1111-1111-1111", "MASTER_KEY")
// File operations
env.Load(".env")
env.Save("backup.env", []string{"API_KEY", "DATABASE_URL"})
// Struct binding
type Config struct {
Port int `env:"PORT"`
Host string `env:"HOST"`
Debug bool `env:"DEBUG"`
Features []string `env:"FEATURES"`
}
var config Config
env.BindJSON("env", &config)
Migration from other packages:
// From os package
value := os.Getenv("KEY") // becomes: env.Get("KEY")
os.Setenv("KEY", "value") // becomes: env.Set("KEY", "value")
// From godotenv
godotenv.Load() // becomes: env.Load()
// From caarlos0/env
env.Parse(&config) // becomes: env.BindJSON("env", &config)
// From viper
viper.GetString("key") // becomes: env.Get("KEY")
viper.ReadInConfig() // becomes: env.Load("config.env")
Index ΒΆ
- func Bind(out interface{}, options BindingOptions) error
- func BindJSON(tag string, out interface{}) error
- func ClearAllCaches()
- func ClearTypeCache()
- func Get(key string, defaultValue ...string) string
- func GetB64(key string, defaultValue ...string) (string, error)
- func GetCacheStats() map[string]interface{}
- func GetSealed(key string, keySource string, defaultValue ...string) (string, error)
- func Load(filename ...string) error
- func LoadFromFile(filename string, out interface{}) error
- func Lookup(key string) (string, bool)
- func MustLoad(filename ...string)
- func ResetCacheCounters()
- func Save(filename string, keys []string) error
- func SaveToFile(filename string, data interface{}) error
- func Set(key, value string) error
- func SetB64(key, value string) error
- func SetSealed(key, value, keySource string) error
- func Unset(key string) error
- type BindingOptions
- type ConfigFormat
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func Bind ΒΆ
func Bind(out interface{}, options BindingOptions) error
Bind binds environment variables to struct fields with configurable options. Optimized version with reflection caching and reduced allocations. Example usage:
type Config struct {
Port int `env:"PORT,default=8080"`
Host string `env:"HOST,default=localhost"`
Debug bool `env:"DEBUG,default=false"`
Features []string `env:"FEATURES"`
Timeout time.Duration `env:"TIMEOUT,default=30s"`
}
var config Config
err := env.Bind(&config, env.DefaultBindingOptions())
func BindJSON ΒΆ
BindJSON binds environment variables to struct fields using the "env" tag. This is a simplified version of Bind for backward compatibility.
func ClearTypeCache ΒΆ
func ClearTypeCache()
ClearTypeCache clears the reflection cache (useful for testing or memory management)
func Get ΒΆ
Get retrieves the value of an environment variable. If the variable does not exist, it returns the provided default value.
func GetB64 ΒΆ
GetB64 retrieves a base64-encoded environment variable and decodes it. If the variable does not exist, it returns the provided default value. Returns an error if the value is not valid base64.
func GetCacheStats ΒΆ
func GetCacheStats() map[string]interface{}
GetCacheStats returns statistics about cache usage for monitoring
func GetSealed ΒΆ
GetSealed retrieves and decrypts a sealed (encrypted) environment variable. The encryption key is derived from the provided keySource environment variable. If keySource is empty, it defaults to "ENV_SEAL_KEY".
func LoadFromFile ΒΆ
LoadFromFile loads configuration from a file in various formats and optionally binds it to a struct using environment variable overrides.
func Lookup ΒΆ
Lookup retrieves the value of an environment variable. It returns the value and a boolean indicating whether the variable exists.
func MustLoad ΒΆ
func MustLoad(filename ...string)
MustLoadOptimized is the optimized version of MustLoad
func ResetCacheCounters ΒΆ
func ResetCacheCounters()
ResetCacheCounters resets all performance counters
func SaveToFile ΒΆ
SaveToFile saves configuration to a file in the specified format
func Set ΒΆ
Set sets an environment variable to the specified value. It returns an error if the operation fails.
func SetB64 ΒΆ
SetB64 base64-encodes a value and sets it as an environment variable. It returns an error if the operation fails.
Types ΒΆ
type BindingOptions ΒΆ
type BindingOptions struct {
Tag string // Tag name to look for (default: "env")
Required bool // Whether to fail if required fields are missing
Prefix string // Prefix to add to all environment variable names
}
BindingOptions provides configuration for struct binding
func DefaultBindingOptions ΒΆ
func DefaultBindingOptions() BindingOptions
DefaultBindingOptions returns default binding options
type ConfigFormat ΒΆ
type ConfigFormat int
ConfigFormat represents supported configuration file formats
const ( FormatJSON ConfigFormat = iota FormatYAML FormatTOML FormatINI FormatEnv )
func (ConfigFormat) String ΒΆ
func (f ConfigFormat) String() string
String returns the string representation of the format
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
examples
|
|
|
advanced-binding
command
|
|
|
base64
command
|
|
|
basic
command
|
|
|
file-operations
command
|
|
|
real-world-app
command
|
|
|
sealed-secrets
command
|