Update README.md
This commit is contained in:
parent
ba76bf17fb
commit
9e933b3024
38
README.md
38
README.md
@ -48,11 +48,11 @@ Using **default** keyword in structure tags to define default value:
|
|||||||
Like parsing JSON object, using **json** keyword to define configuration name:
|
Like parsing JSON object, using **json** keyword to define configuration name:
|
||||||
```golang
|
```golang
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Host string `json:"host"`
|
Host string `json:"host"`
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
Username string `json:"username" default:"admin"`
|
Username string `json:"username" default:"admin"`
|
||||||
Password string `json:"password" default:"admin"`
|
Password string `json:"password" default:"admin"`
|
||||||
Log Log `json:"log"`
|
Log Log `json:"log"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -74,11 +74,11 @@ Corresponding JSON file:
|
|||||||
Like parsing Yaml object, using **yaml** keyword to define configuration name
|
Like parsing Yaml object, using **yaml** keyword to define configuration name
|
||||||
```golang
|
```golang
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
Username string `yaml:"username" default:"admin"`
|
Username string `yaml:"username" default:"admin"`
|
||||||
Password string `yaml:"password" default:"admin"`
|
Password string `yaml:"password" default:"admin"`
|
||||||
Log Log `yaml:"log"`
|
Log Log `yaml:"log"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Corresponding Yaml file:
|
Corresponding Yaml file:
|
||||||
@ -96,11 +96,11 @@ Corresponding Yaml file:
|
|||||||
Using **env** keyword to define configuration name
|
Using **env** keyword to define configuration name
|
||||||
```golang
|
```golang
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Host string `env:"DB_HOST"`
|
Host string `env:"DB_HOST"`
|
||||||
Port int `env:"DB_PORT"`
|
Port int `env:"DB_PORT"`
|
||||||
Username string `env:"DB_USER" default:"admin"`
|
Username string `env:"DB_USER" default:"admin"`
|
||||||
Password string `env:"DB_PASSWORD" default:"admin"`
|
Password string `env:"DB_PASSWORD" default:"admin"`
|
||||||
Log Log `env:"DB_LOG_"`
|
Log Log `env:"DB_LOG_"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -119,11 +119,11 @@ Since the ```Log``` is a structure and nested in ```Database``` structure, the t
|
|||||||
Using **cli** keyword to define configuration name
|
Using **cli** keyword to define configuration name
|
||||||
```golang
|
```golang
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Host string `cli:"host database host name"`
|
Host string `cli:"host database host name"`
|
||||||
Port int `cli:"port database port"`
|
Port int `cli:"port database port"`
|
||||||
Username string `cli:"username database username" default:"admin"`
|
Username string `cli:"username database username" default:"admin"`
|
||||||
Password string `cli:"password database password" default:"admin"`
|
Password string `cli:"password database password" default:"admin"`
|
||||||
Log Log `cli:"log database log configurations"`
|
Log Log `cli:"log database log configurations"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
For **cli** definition, the string before the first space is command line argument, the rest string are the command line usage and will be oupputed when printing usage
|
For **cli** definition, the string before the first space is command line argument, the rest string are the command line usage and will be oupputed when printing usage
|
||||||
@ -226,17 +226,17 @@ Then, you can parse as below:
|
|||||||
dbConfig := Database{}
|
dbConfig := Database{}
|
||||||
|
|
||||||
// parse default values
|
// parse default values
|
||||||
if err := config.ParseDefault(&dbConfig); err != nil {
|
if err := config.ParseDefault(&dbConfig); err != nil {
|
||||||
// error handling
|
// error handling
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse configuration file from command line
|
// parse configuration file from command line
|
||||||
err := config.ParseConfig(&dbConfig, "c")
|
err := config.ParseConfig(&dbConfig, "c")
|
||||||
|
|
||||||
// parse default configurations
|
// parse default configurations
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = config.ParseConfigFile(&dbConfig), "")
|
err = config.ParseConfigFile(&dbConfig), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse environment variables
|
// parse environment variables
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user