Goconf
Configuration loader in Go
Install / Use
/learn @timestee/GoconfREADME
[Archived] This repository has been archived,See https://github.com/sandwich-go/xconf instead.
goconf
Overview
- Read configuration automatically based on the given struct's field name.
- Load configuration from multiple sources
- file inherit
Values are resolved with the following priorities (lowest to highest):
- Options struct default value
- Flags default value
- Config file value, TOML or JSON file
- OS Env
- Command line flag
About field tags in structs
type TestOptions struct {
Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"`
}
flagis the name passed from the command line.cfgis the name used in config files.defaultis the default value
If do not define flag tag, flag will be snake case of the fild name.
If do not define cfg tag, cfg value will be flag value.
For example, flag and cfg will be http_address.
HTTPAddress string
Usage
load multiple config files
package main
import "github.com/timestee/goconf"
type TestOptions struct {
goconf.AutoOptions
HTTPAddress string `default:"0.0.0.0:0000"`
Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"`
LogLevel int `default:"3"`
BoolVar bool `default:"false"`
}
func main() {
ops := &TestOptions{}
goconf.MustResolve(ops,"conf_1.toml","conf_2.toml")
}
go run main.go --log_level=1
The output will be:
[Config] auto flag succ, name: _auto_conf_files_ val:
[Config] auto flag succ, name: http_address val: 0.0.0.0:0000
[Config] auto flag fail, name: hosts val: 127.0.0.0,127.0.0.1 err: type not support []string
[Config] auto flag succ, name: log_level val: 3
[Config] auto flag succ, name: bool_var val: false
[Config] file: [conf_1.toml conf_2.toml]
[Config] load: conf_1.toml
[Config] load: conf_2.toml
[Config]
{
"AutoConfFiles": "",
"HTTPAddress": "127.0.0.1:2",
"Hosts": [
"10.0.61.29",
"10.0.61.30",
"10.0.61.31",
"10.0.61.32"
],
"LogLevel": 1,
"BoolVar": true
}
load config file with file inherited
package main
import "github.com/timestee/goconf"
type TestOptions struct {
goconf.AutoOptions
HTTPAddress string `default:"0.0.0.0:0000"`
Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"`
LogLevel int `default:"3"`
BoolVar bool `default:"false"`
}
func main() {
ops := &TestOptions{}
// conf_3 inherit from conf_1 and conf_2
goconf.MustResolve(ops,"conf_3.toml")
}
go run main.go --http_address=0.0.0.0:1111111
The output will be:
[Config] auto flag succ, name: _auto_conf_files_ val:
[Config] auto flag succ, name: http_address val: 0.0.0.0:0000
[Config] auto flag fail, name: hosts val: 127.0.0.0,127.0.0.1 err: type not support []string
[Config] auto flag succ, name: log_level val: 3
[Config] auto flag succ, name: bool_var val: false
[Config] file: [conf_3.toml]
[Config] load: ./conf_1.toml
[Config] load: ./conf_2.toml
[Config] load: conf_3.toml
[Config]
{
"AutoConfFiles": "",
"HTTPAddress": "0.0.0.0:1111111",
"Hosts": [
"10.0.61.29",
"10.0.61.30",
"10.0.61.31",
"10.0.61.32",
"10.0.61.33"
],
"LogLevel": 2,
"BoolVar": true
}
Related Skills
node-connect
353.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
353.3kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
111.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
353.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
