项目作者: gadavy

项目描述 :
Simple JSON configer for Go.
高级语言: Go
项目地址: git://github.com/gadavy/configer.git
创建时间: 2019-07-29T16:10:20Z
项目社区:https://github.com/gadavy/configer

开源协议:

下载


Simple JSON configer for Go.

Example

To create config file you can use two methods:

  • With creating a configurator:
    ``` go
    import (
    “log”
    “github.com/TermiusOne/configer”
    )

type MyConfig struct {
DB MyConfigDB json:"database"
ServerHost string json:"host"
ServerPort int json:"port"
}

type MyConfigDB struct {
User string json:"user"
Password string json:"password"
Address string json:"address"
Database string json:"database"
Charset string json:"charset"
}

func main() {
conf := &MyConfig{}

  1. configManger := configer.New("config/config.json", conf)
  2. err := configManger.Create()
  3. if err != nil {
  4. log.Fatal(err)
  5. }

}

  1. * Or using function `CreateConfig`:
  2. ``` go
  3. func main() {
  4. conf := &MyConfig{}
  5. err := configer.CreateConfig("config/config.json", conf)
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. }
  • You will receive this file:
    1. {
    2. "database": {
    3. "user": "",
    4. "password": "",
    5. "address": "",
    6. "database": "",
    7. "charset": ""
    8. },
    9. "host": "",
    10. "port": 0
    11. }

You can also use two methods to read config file:

  1. func main() {
  2. conf := &MyConfig{}
  3. configManger := configer.New("config/config.json", conf)
  4. err := configManger.Read()
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. }
  • or

    1. func main() {
    2. conf := &MyConfig{}
    3. err := configer.ReadConfig("config/config.json", conf)
    4. if err != nil {
    5. log.Fatal(err)
    6. }
    7. }

First method with create configurator approached if you want to create a config, add some data and use it.