Update README.md
This commit is contained in:
parent
e14ed1d5bc
commit
8f8a3d7464
32
README.md
32
README.md
@ -1,5 +1,5 @@
|
|||||||
## Introduction
|
## 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](https://github.com/go-yaml/yaml) to anlayze Yaml file and use built-in golang library to handle JSON file.
|
**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](https://github.com/go-yaml/yaml) to anlayze Yaml file and uses built-in golang library to handle JSON file.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
1. Install [Yaml](https://github.com/go-yaml/yaml) library first:
|
1. Install [Yaml](https://github.com/go-yaml/yaml) library first:
|
||||||
@ -13,17 +13,35 @@ go get github.com/eschao/config
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
#### Defines **default** values
|
#### 1. Defines **default** values
|
||||||
**config** library supports defining a default value for structure members by using **default** keyword in structure tags
|
**config** library supports defining a default value for structure members by using **default** keyword in structure tags
|
||||||
```golang
|
```golang
|
||||||
type Database struct {
|
type Log struct {
|
||||||
Username string `default:"admin"`
|
Path string `default:"/var/logs"`
|
||||||
Password string `default:"admin"`
|
Level string `default:"debug"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
After specified default value in tags, calls ```config.ParseDefault(interface{})``` to set it on structure instance, example codes as the below:
|
After that, calls ```config.ParseDefault(interface{})``` to set it on structure instance, example codes as the below:
|
||||||
|
```golang
|
||||||
|
logConfig := Log{}
|
||||||
|
config.ParseDefault(&logConfig)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Reads configurations from JSON file
|
||||||
|
Like analyzing JSON object from file, reading configurations from JSON file is also simple:
|
||||||
|
```golang
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Parsing configurations from JSON:
|
||||||
```golang
|
```golang
|
||||||
dbConfig := Database{}
|
dbConfig := Database{}
|
||||||
config.ParseDefault(&dbConfig)
|
config.ParseConfigFile(&dbConfig, "config.json")
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user