github.com/lingyao2333/mo-zero@v1.4.1/core/conf/readme.md (about)

     1  ## How to use
     2  
     3  1. Define a config structure, like below:
     4  
     5  ```go
     6  type RestfulConf struct {
     7  	Host         string        `json:",default=0.0.0.0"`
     8  	Port         int
     9  	LogMode      string        `json:",options=[file,console]"`
    10  	Verbose      bool          `json:",optional"`
    11  	MaxConns     int           `json:",default=10000"`
    12  	MaxBytes     int64         `json:",default=1048576"`
    13  	Timeout      time.Duration `json:",default=3s"`
    14  	CpuThreshold int64         `json:",default=900,range=[0:1000]"`
    15  }
    16  ```
    17  
    18  2. Write the yaml, toml or json config file:
    19  
    20  - yaml example
    21  
    22  ```yaml
    23  # most fields are optional or have default values
    24  Port: 8080
    25  LogMode: console
    26  # you can use env settings
    27  MaxBytes: ${MAX_BYTES}
    28  ```
    29  
    30  - toml example
    31  
    32  ```toml
    33  # most fields are optional or have default values
    34  Port = 8_080
    35  LogMode = "console"
    36  # you can use env settings
    37  MaxBytes = "${MAX_BYTES}"
    38  ```
    39  
    40  3. Load the config from a file:
    41  
    42  ```go
    43  // exit on error
    44  var config RestfulConf
    45  conf.MustLoad(configFile, &config)
    46  
    47  // or handle the error on your own
    48  var config RestfulConf
    49  if err := conf.Load(configFile, &config); err != nil {
    50    log.Fatal(err)
    51  }
    52  
    53  // enable reading from environments
    54  var config RestfulConf
    55  conf.MustLoad(configFile, &config, conf.UseEnv())
    56  ```
    57