https://github.com/fifsky/goconf
Gjson-based configuration file
https://github.com/fifsky/goconf
go
Last synced: 1 day ago
JSON representation
Gjson-based configuration file
- Host: GitHub
- URL: https://github.com/fifsky/goconf
- Owner: fifsky
- License: mit
- Created: 2018-05-30T07:14:01.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-17T02:24:26.000Z (over 7 years ago)
- Last Synced: 2025-03-02T01:45:26.811Z (over 1 year ago)
- Topics: go
- Language: Go
- Size: 19.5 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Gjson-based configuration file for golang
GoConf is a Go package that quickly retrieves JSON configuration files based on [GJSON](https://github.com/tidwall/gjson/)
Getting Started
===============
## Installing
To start using GoConf, install Go and run `go get`:
```sh
$ 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}
]
}
```
```go
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
```go
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](https://github.com/tidwall/gjson/blob/master/README.md#path-syntax)
## 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
```go
conf.MustGet("dev.notfound").String() //value is empty string
conf.MustGet("dev2.not").Int() //value is 0 int
```
## Unmarshal to Struct
```go
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
```go
database := &Database{}
conf.Unmarshl("json5.database",database)
```
## Contact
Xudong Cai [@fifsky](https://fifsky.com/)
## License
GoConf source code is available under the MIT [License](/LICENSE).