2
0
Golang library for reading configurations from JSON, YAML files, environment variables and command line.
Go to file
2017-12-13 00:27:48 +08:00
cli add comments 2017-12-12 16:00:34 +08:00
env add comments 2017-12-12 16:00:34 +08:00
test add comments 2017-12-12 16:00:34 +08:00
utils add comments 2017-12-12 16:00:34 +08:00
.gitignore Pre 1.0 version 2017-12-11 17:14:53 +08:00
config_test.go add comments 2017-12-12 16:00:34 +08:00
config.go add comments 2017-12-12 16:00:34 +08:00
LICENSE Initial commit 2017-12-06 17:11:17 +08:00
README.md Update README.md 2017-12-13 00:27:48 +08:00

Introduction

config is a simple golang library and designed to read configurations from JSON, Yaml files, environment variables and command line. config depends on go-yaml to anlayze Yaml file and uses built-in golang library to handle JSON file.

Installation

  1. Install Yaml library first:
go get gopkg.in/yaml.v2
  1. Install config library:
go get github.com/eschao/config

Usage

I. Data types

Like JSON, Yaml, config uses tags to define configurations. It supports the following golang data types:

  • bool
  • string
  • int8, int16, int, int32, int64
  • uint8, uint16, uint, uint32, uint64
  • float32, float64
  • slice type. e.g: []string, []int ...

II. Defines default values

Using default keyword in structure tags to define default value. Example codes:

  type Log struct {
    Path  string `default:"/var/logs"`
    Level string `default:"debug"`
  }

After that, calls config.ParseDefault(interface{}) to set structure instance with default values. Example codes:

  logConfig := Log{}
  config.ParseDefault(&logConfig)

III. Reads configurations from JSON and Yaml files

1. Using json to define configuration name
  type Database struct {
    Host string     `json:"host"`
    Port int        `json:"port"`
    Username string `json:"username" default:"admin"`
    Password string `json:"password" default:"admin"`
    Log Log         `json:"log"`
  }
2. Using yaml to define configuration name
  type Database struct {
    Host string     `yaml:"host"`
    Port int        `yaml:"port"`
    Username string `yaml:"username" default:"admin"`
    Password string `yaml:"password" default:"admin"`
    Log Log         `yaml:"log"`
  }

Parsing configurations from JSON:

  dbConfig := Database{}
  config.ParseConfigFile(&dbConfig, "config.json")