Skip to content

luizvnasc/gonfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub license Build Status Go Report Card Coverage Status

Gonfig

A simple module to load configurations file to a struct.

Getting Started

To install this package run:

go get github.com/luizvnasc/gonfig

Example

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.

Supported formats

Format is supported?
json ✔️
xml ✔️
yaml ✔️
toml ✔️
environment variables ✔️

Dependencies

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

Authors

  • Luiz Augusto Volpi Nascimento - Initial work - @luizvnasc

License

This project is licensed under the MIT License - see the LICENSE file for details