gonfig
Описание
Gonfig is a flexible library for simplifying configuration management. It supports loading settings from environment variables, flags, and various file formats. The library is extensible, allowing new formats to be added, and key components can be replaced, such as using `spf13/pflag` instead of the standard `flag` package.
Языки
- Go100%

What is gonfig?
gonfig is a flexible and extensible configuration library designed to simplify working with application settings.
It supports loading configurations from environment variables, command-line flags, and various configuration file formats.
Additionally, it offers an easy way to extend support for new formats. One of its key features is the ability to replace
or customize components, such as using instead of the standard package from the Go standard library.
This library simplifies configuration management, making it easy to define, override, and merge settings in your applications.
Why Use Gonfig?
- Multiple Sources – Load configurations from config-files, flags, environment variables, or custom sources.
- Easy to Use – Simple API for defining and managing configurations.
- Extensible – You can implement custom loaders to fit your needs.
- Lightweight – No unnecessary dependencies, optimized for performance.
General Priority Hierarchy:
The priority described below is considered the default priority and can be modified through configuration settings.
-
Defaults — These are basic configuration values embedded in the application's code. They ensure the application can run even if no external configurations are provided.
-
Environment Variables — Environment variables are usually used to configure deployment-related parameters (e.g., logins, ports, database addresses). These variables often have a higher priority as they can be dynamically set depending on the environment.
-
Flags — Command-line flags usually have the highest priority since they allow direct overriding of any settings at application startup. This is useful when a quick configuration change is needed without modifying the code or config files.
-
Config File — A configuration file stored on disk, typically containing predefined parameters for a specific environment. This can be in formats like JSON, YAML, TOML, etc.
-
Remote Config — This is a configuration retrieved from external sources, such as configuration servers or cloud services (e.g., Consul, Etcd, or AWS SSM). These systems usually allow centralized management of settings across different applications.
Installation
Note: gonfig uses Go Modules to manage dependencies.
Explain Hierarchy:
- Defaults — Set in the code. For example, the default server port is
.8080 - Environment Variables — Environment variables can be used to set database connections or other services linked to the environment.
- Flags — Command-line arguments always have the highest priority, as they can be specified at application startup to override any other parameter.
- Config File — The configuration file specifies more detailed parameters, such as database connections or the application's operating mode.
- Remote Config — Configuration retrieved from a remote server can override settings from the config file.
Current status
- Load defaults
- Load environments
- Load flags
- Mark as required
- Load YAML
- Load JSON (you can use custom loader)
- Load TOML (you can use custom loader)
- Other formats, you can write it using custom loader
Examples
Simple
Extended (using New.Load)
Use YAML loader
Custom Loaders
You can implement your own configuration loaders by implementing the interface.
or with config-path defining