As a developer, I’ve experimented with various configuration formats over the years, including JSON, YAML, and properties files, among others. However, in recent times, I find myself increasingly relying on environment variables to manage configuration values.
In today’s cloud-centric world, where stateless services are the norm, environment variables are an excellent fit, especially when working with containerization technologies like Docker or Kubernetes.
- godotenv is a package that loads .env files if they exist. This is useful in local development environments, where you may not want to store sensitive configuration data in code.
- envconfig, on the other hand, “loads” existing environment variables into a struct. It supports the use of struct tags to specify alternate, default, and required environment variables.
This code defines a
Config struct that includes three configuration values, namely
LoadConfig function uses
godotenv to load any values specified in a local
.env file (if one exists), and
envconfig to populate the
Config struct with any environment variables that match the struct tags.
Once you’ve successfully loaded your configuration struct, you can initialize your config as follows:
In this example,
main.go simply calls
LoadConfig and prints out the value of the
PostgresDSN configuration variable.
By leveraging environment variables and these two Go packages, you can create more portable, scalable, and secure applications that can be easily configured for different environments.