gostructor

Two-tag configuration

Back to features

gostructor fills a struct’s fields from two small, cleanly separated tags:

With no options, Configure uses a minimal default source list — Env then Default — so an environment variable beats the baked-in default. A configured field that no source can resolve is a hard error (unless it’s gos:"optional"): surprises are errors.

type Config struct {
	Host  string `cfg:"host" gos:"default:0.0.0.0"`
	Port  int    `cfg:"port" gos:"default:8080"`
	Debug bool   `cfg:"debug" gos:"default:false"`
}

cfg, err := gostructor.Configure(&Config{})

Try it

The basic example sets PORT=9090 in the process, then calls Configure with no options. The env source names its variable from the base name (portPORT), so the env value wins over the gos default; host and debug fall back to their defaults.

go run ./examples/basic

Output:

&{Host:0.0.0.0 Port:9090 Debug:false}

Port is 9090 (from the env var), while Host and Debug keep their gos:"default:..." values because nothing else supplied them.