Skip to content

Load environmental variable values to go struct directly

License

Notifications You must be signed in to change notification settings

wgarunap/goconf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Config

Library to load env configuration

How to use it

package main
import (

"errors"
"github.com/caarlos0/env/v6"
"github.com/wgarunap/goconf"
"log"
)

type Conf struct {
	Name string `env:"MY_NAME"`
}

var Config Conf

func (Conf) Register()error {
	return env.Parse(&Config)
}

func (Conf) Validate() error{
	if Config.Name == "" {
		return errors.New(`MY_NAME environmental variable cannot be empty`)
	}
    return nil
}

func (Conf) Print() interface{} {
	return Config
}

func main() {
	err := goconf.Load(
		new(Conf),
	)
    if err != nil{
           log.Fatal(err)
    }
    log.Print(`configuration loaded, `,Config.Name)
}