How I configure my Go Applications

In this article I will show you what I think is the best way to configure your Go Application.

Image for post
Image for post

In the past I’ve been trying a lot of configuration formats like JSON, YAML, properties and much more. But in the recent past I see myself just using environment variables.

In the modern cloud age where we rely heavily on stateless services environment variables are a good fit, especially when using Docker or Kubernetes.

To simplify my code for loading configurations, I make use of godotenv and envconfig.

- godotenv loads .env files if they exist, what they usually do in my local development environments.
- envconfig “loads” existing environment variables into a struct. It supports the use of struct tags to specify alternate, default, and required environment variables.

If you have sucessfully built your configuration struct you can initialize your config as following:

Your application now loads .env files if provided or existing environment variables.

I’m happy if I could help you get some inspiration or solve your problem.

This is my first article I’ve ever published, and I’m not a native English speaker, so I’m sorry if there are some mistakes. If you found something that needs to be corrected I’d be grateful if you message me.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store