Documentation
ยถ
Overview ยถ
Package konfig provides a Spring Framework-inspired configuration management system for Go applications.
konfig supports YAML-based configuration files, profile-specific configurations, environment variable substitution, and struct-based configuration loading with type safety.
Basic usage:
err := konfig.Load() if err != nil { log.Fatal(err) } port := os.Getenv("server.port")
Struct-based configuration:
type Config struct { Port string `konfig:"server.port" default:"8080"` } var cfg Config err := konfig.LoadInto(&cfg)
Index ยถ
- func ClearEnv()
- func GetEnv(key string) string
- func GetProfile() string
- func IsDevProfile() bool
- func IsProdProfile() bool
- func IsProfile(profile string) bool
- func Load() error
- func LoadFrom(pathToConfigFile string) error
- func LoadInto(config interface{}) error
- func ResetProfileInitialized()
- func SetEnv(key string, value string) error
Constants ยถ
This section is empty.
Variables ยถ
This section is empty.
Functions ยถ
func ClearEnv ยถ
func ClearEnv()
ClearEnv deletes all environment variables. This is primarily used for testing purposes and should be used with caution.
func GetEnv ยถ
GetEnv retrieves the value of the environment variable named by the key. This is a convenience wrapper around os.Getenv for consistency with konfig's API.
func GetProfile ยถ
func GetProfile() string
GetProfile returns the currently active profile name.
The profile is determined by command-line flags (-p or --profile). Returns an empty string if no profile is active.
Example:
profile := konfig.GetProfile() if profile != "" { fmt.Printf("Active profile: %s\n", profile) } else { fmt.Println("No active profile") }
func IsDevProfile ยถ
func IsDevProfile() bool
IsDevProfile returns true if the current active profile is "dev".
The profile is determined by command-line flags (-p or --profile).
Example:
if konfig.IsDevProfile() { // Development-specific logic fmt.Println("Running in development mode") }
func IsProdProfile ยถ
func IsProdProfile() bool
IsProdProfile returns true if the current active profile is "prod".
The profile is determined by command-line flags (-p or --profile).
Example:
if konfig.IsProdProfile() { // Production-specific logic fmt.Println("Running in production mode") }
func IsProfile ยถ
IsProfile returns true if the current active profile matches the given name.
This is useful for checking custom profile names beyond "dev" and "prod".
Example:
if konfig.IsProfile("staging") { // Staging-specific logic fmt.Println("Running in staging mode") }
func Load ยถ
func Load() error
Load initializes konfig by loading configuration from default sources.
It first loads the base configuration file (resources/application.yaml or .yml), then loads any active profile-specific configuration (e.g., resources/application-dev.yaml). Profile-specific values override base configuration values.
Configuration keys are flattened into dot notation and set as environment variables, making them accessible via os.Getenv() or the GetEnv() function.
The active profile is determined by command-line flags (-p or --profile).
Example:
err := konfig.Load() if err != nil { log.Fatal("Failed to load configuration:", err) } port := os.Getenv("server.port") dbURL := os.Getenv("database.url")
func LoadFrom ยถ
LoadFrom loads configuration from a specific YAML file.
Unlike Load(), this function only loads the specified file and does not attempt to load default application.yaml or profile-specific files.
The configuration keys are flattened and set as environment variables, just like with Load().
Example:
err := konfig.LoadFrom("config/custom.yaml") if err != nil { log.Fatal("Failed to load custom config:", err) } customValue := os.Getenv("custom.setting")
func LoadInto ยถ
func LoadInto(config interface{}) error
LoadInto loads configuration into a Go struct using struct tags for type-safe configuration access.
This function first calls Load() to initialize konfig, then uses reflection to populate the provided struct based on `konfig` and `default` struct tags.
Struct Tag Usage:
- `konfig:"key.path"` - Maps the field to a configuration key (supports dot notation)
- `default:"value"` - Provides a default value if the configuration key is not set
- Fields without `konfig` tags are ignored
The config parameter must be a pointer to a struct. Nested structs are supported and will be populated recursively.
Example:
type DatabaseConfig struct { Host string `konfig:"host" default:"localhost"` Port string `konfig:"port" default:"5432"` Password string `konfig:"password"` } type Config struct { AppName string `konfig:"app.name" default:"MyApp"` Database DatabaseConfig `konfig:"database"` } var cfg Config err := konfig.LoadInto(&cfg) if err != nil { log.Fatal("Failed to load config:", err) } fmt.Printf("App: %s, DB: %s:%s\n", cfg.AppName, cfg.Database.Host, cfg.Database.Port)
func ResetProfileInitialized ยถ
func ResetProfileInitialized()
ResetProfileInitialized resets the profile initialization state. This function is primarily used in tests to ensure clean state between test runs.
Types ยถ
This section is empty.