Add flexibility to the way you configure your applications. Load it from files as JSON, YAML and TOML, command line flags, environment variables and more. This package is a wrapper for koanf.
go get -u github.com/americanas-go/config
# config.yaml
app:
application:
name: app_example_file
package main
import (
"log"
"github.com/americanas-go/config"
)
func init() {
config.Add("app.application.name", "app_example_file", "description of name")
}
func main() {
config.Load()
log.Println(config.String("app.application.name"))
//output: 2021/05/28 16:52:49 app_example_file
}
package main
import (
"log"
"github.com/americanas-go/config"
)
func init() {
config.Add("k.env", "env", "description of env")
config.Add("k.camelCase", "camel-case", "description of camel case")
config.Add("k.camelCaseTwo", "camel_case_two", "description of camel case two")
}
func main() {
config.Load()
log.Println(config.String("k.env"))
//output: 2021/05/28 17:20:03 env
log.Println(config.String("k.camelCase"))
//output: 2021/05/28 17:20:03 camel-case
log.Println(config.String("k.camelCaseTwo"))
//output: 2021/05/28 17:20:03 camel_case_two
}
package main
import (
"log"
"github.com/americanas-go/config"
)
func init() {
config.Add("app.application.name", "app_example_file", "description of name")
config.Add("app.application.enabled", true, "description of enabled")
config.Add("app.application.duration", 10, "description of duration")
}
func main() {
config.Load()
log.Println(config.String("app.application.name"))
//output: 2021/05/28 17:40:27 app_example_file
log.Println(config.Bool("app.application.enabled"))
//output: 2021/05/28 17:40:27 true
log.Println(config.Int("app.application.duration"))
//output: 2021/05/28 17:40:27 10
}
# config.yaml
app:
application:
name: app_example_file
enabled: true
duration: 10
package main
import (
"log"
"github.com/americanas-go/config"
)
type AppConfig struct {
Application struct {
Name string
Enabled bool
Duration int
}
}
func init() {
config.Add("app.application.name", "app_example_file", "description of name")
config.Add("app.application.enabled", true, "description of enabled")
config.Add("app.application.duration", 10, "description of duration")
}
func main() {
config.Load()
c := AppConfig{}
config.UnmarshalWithPath("app", &c)
log.Println(c.Application.Name)
//output: 2021/05/28 17:54:12 app_example_file
log.Println(c.Application.Enabled)
//output: 2021/05/28 17:54:12 true
log.Println(c.Application.Duration)
//output: 2021/05/28 17:54:12 10
}
Every help is always welcome. Feel free do throw us a pull request, we'll do our best to check it out as soon as possible. But before that, let us establish some guidelines:
- This is an open source project so please do not add any proprietary code or infringe any copyright of any sort.
- Avoid unnecessary dependencies or messing up go.mod file.
- Be aware of golang coding style. Use a lint to help you out.
- Add tests to cover your contribution.
- Add godoc to your code.
- Use meaningful messages to your commits.
- Use pull requests.
- At last, but also important, be kind and polite with the community.
Any submitted issue which disrespect one or more guidelines above, will be discarded and closed.
Released under the MIT License.