How to properly manage configuration in Go: env, flags or files?
Hi everyone! I'm new to Go and trying to figure out how to properly store application settings. I started writing a small web server, and I ran into a problem: I want the application to be configurable without recompilation — to change the port, database connection string, logging level.
So far I'm doing this: I create a config package where I read environment variables in init(). But this seems like a hack, especially when there are many settings (about 20). Plus I don't know how to set default values and how to conveniently pass the config to other packages.
Here's my current code:
```go
package config
import "os"
var Port string
var DBHost string
func init() {
Port = os.Getenv("PORT")
if Port == "" {
Port = "8080"
}
DBHost = os.Getenv("DB_HOST")
if DBHost == "" {
No answers yet.
Log in to leave an answer
Blogs