configor配置文件工具

1、概述

  一个支持 yaml、json、toml、shell 的配置文件工具。

2、安装

go get github.com/jinzhu/configor

3、使用

1)创建一个yaml文件。

appname: test
db:
  name:     test
  user:     root
  password: 123
  port:     3306

contacts:
  - name:  jack
    email: jack@test.com
  - name:  tom
    email: tom@test.com

2)代码

package main

import (
    "fmt"
    "github.com/jinzhu/configor"
)

type Config struct {
    APPName string `default:"app name"`
    DB struct{
        Name     string
        User     string `default:"root"`
        Password string `required:"true" env:"DBPassword"`
        Port     uint   `default:"3306"`
    }
    Contacts []struct{
        Name  string
        Email string `required:"true"`
    }
}

func main()  {
    var conf = Config{}
    err := configor.Load(&conf, "config.yml")
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v \n", conf)
}

  

参考:Golang Configor 配置文件工具 - 简书 (jianshu.com)

 

 

posted @ 2022-06-23 16:49  ☞@_@  阅读(136)  评论(0编辑  收藏  举报