Categorygithub.com/fifsky/goconf
modulepackage
1.0.1
Repository: https://github.com/fifsky/goconf.git
Documentation: pkg.go.dev

# README

GJSON
Build Status codecov Go Report Card
GoDoc

Gjson-based configuration file for golang

GoConf is a Go package that quickly retrieves JSON configuration files based on GJSON

Getting Started

Installing

To start using GoConf, install Go and run go get:

$ go get -u github.com/fifsky/goconf

This will retrieve the library.

Load multiple configs

You only need to declare the Tag conf:"filename" on the struct

type log struct {
    LogName string `json:"log_name"`
    LogPath string `json:"log_path"`
}

type db struct {
    Name string `json:"name"`
    Host string `json:"host"`
    Port string `json:"port"`
}

type config struct {
    Log log `conf:"log"`
    DB  db  `conf:"db"`
}

conf, err := NewConfig("./testdata/")
if err != nil {
    fmt.Fatal(err)
}

app := &config{}
err = conf.Load(app)

fmt.Printf("%#v",app)

Get config a value

Config file: testdata/dev.json

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "fav.movie": "Deer Hunter",
  "friends": [
    {"first": "Dale", "last": "Murphy", "age": 44},
    {"first": "Roger", "last": "Craig", "age": 68},
    {"first": "Jane", "last": "Murphy", "age": 47}
  ]
}
package main

import "github.com/fifsky/goconf"

func main() {
    conf, _ := goconf.NewConfig("./testdata/")
    ret, _ := conf.Get("dev.name.last")

    println(ret.String())
}

This will print:

Anderson

You can also use the Must function to simplify this

package main

import "github.com/fifsky/goconf"

func main() {
    conf, _ := goconf.NewConfig("./testdata/")
    println(conf.MustGet("dev.name.last").String())
}

first key dev is filename

Path Syntax

For more Path Syntax, see the GJSON document

Default value

If you use the Must function to access a non-existent configuration file or keys, GoConf returns zero value, which is consistent with gjson.result

conf.MustGet("dev.notfound").String() //value is empty string
conf.MustGet("dev2.not").Int() //value is 0 int

Unmarshal to Struct

type Database struct {
	Driver string `json:"driver"`
	Host   string `json:"host"`
	Port   int    `json:"port"`
}

type ConfigDemo struct {
	Database Database `json:"database"`
}

conf, _ := goconf.NewConfig("./testdata/")
app := &ConfigDemo{}
err = conf.Unmarshal("json5", app)
if err != nil {
    fmt.Println(err)
}
fmt.Println(app)
//&{{mysql localhost 3306}}

Unmarshal also supports Xpath

database := &Database{}
conf.Unmarshl("json5.database",database)

Contact

Xudong Cai @fifsky

License

GoConf source code is available under the MIT License.

# Functions

No description provided by the author

# Structs

config file path and ext,Ext default .json.