A simple module to load configurations file to a struct.
To install this package run:
go get github.com/luizvnasc/gonfig
package main
import (
"fmt"
"github.com/luizvnasc/gonfig"
)
//Config struct to store the app configuration
type Config struct {
Version string `toml:"version"`
Description string `toml:"desc"`
Redis struct {
Host string `toml:"host"`
Port uint `toml:"port"`
} `toml:"redis"`
}
func main() {
var config Config
gonfig.Load(&config,"config.toml")
fmt.Printf("%v", config)
}
If you want to use environment variables:
package main
import (
"fmt"
"github.com/luizvnasc/gonfig"
)
//Config struct to store the app configuration
type Config struct {
Version string `env:"VERSION"`
Description string `env:"DESCRIPTION"`
Redis struct {
Host string `env:"REDIS_HOST"`
Port uint `env:"REDIS_PORT"`
}
}
func main() {
var config Config
gonfig.Load(&config)
fmt.Printf("%v", config)
}
You can see more examples here.
Format | is supported? |
---|---|
json | ✔️ |
xml | ✔️ |
yaml | ✔️ |
toml | ✔️ |
environment variables | ✔️ |
Dependency | Repository |
---|---|
gopkg.in/yaml.v3 | https://github.com/go-yaml/yaml |
go-toml | github.com/pelletier/go-toml |
go-env | github.com/luizvnasc/goenv |
- Luiz Augusto Volpi Nascimento - Initial work - @luizvnasc
This project is licensed under the MIT License - see the LICENSE file for details