I'm trying to use the envconfig library, https://github.com/kelseyhightower/envconfig, in a program where most of the configuration variables have default values which I would only like to override if specific environment variables are set.
I've tried to run the following example program:
package main
import (
"fmt"
"log"
"github.com/kelseyhightower/envconfig"
)
type config struct {
foo string `default:"bar"`
}
func main() {
var cfg config
if err := envconfig.Process("app", &cfg); err != nil {
log.Fatalln(err)
}
fmt.Println(cfg.foo)
}
without the APP_FOO environment variable set:
> echo $APP_FOO
However, this does not print bar as I would expect it to:
> go run use_envconfig.go
I don't see how my implementation is different from the documented example at https://github.com/kelseyhightower/envconfig#struct-tag-support to which the following comment pertains:
If envconfig can't find an environment variable value for
MYAPP_DEFAULTVAR, it will populate it with "foobar" as a default value.
How can I make this program so that the cfg's value of foo is bar unless I set the APP_FOO environment variable?