multiconfig, 在 Go,从多个源加载配置

分享于 

4分钟阅读

GitHub

  繁體 雙語
Load configuration from multiple sources in Go
  • 源代码名称:multiconfig
  • 源代码网址:http://www.github.com/koding/multiconfig
  • multiconfig源代码文档
  • multiconfig源代码下载
  • Git URL:
    git://www.github.com/koding/multiconfig.git
    Git Clone代码到本地:
    git clone http://www.github.com/koding/multiconfig
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/koding/multiconfig
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    Multiconfig GoDocBuild Status

    从多个源加载配置。 Multiconfig使来自不同配置源的加载/解析成为一。 任何应用的问题是,随着时间的推移,有很多选项可以填充一组配置。 通过动态创建所有必要选项,Multiconfig可以轻松。 检查下面的示例以查看它的实际情况。

    特性

    Multiconfig可以根据以下源的结构名字段自动读取配置:

    • 结构标记
    • TOML文件
    • JSON文件
    • YAML文件
    • Environment variables
    • 标志

    安装

    
    
    
    
    go get github.com/koding/multiconfig
    
    
    
    

    的用法和示例

    让我们定义和结构定义我们的配置

    
    
    
    
    type Server struct {
    
    
     Name string `required:"true"`
    
    
     Port int `default:"6060"`
    
    
     Enabled bool
    
    
     Users []string
    
    
    }
    
    
    
    

    将配置加载到 multiconfig:

    
    
    
    
    // Create a new DefaultLoader without or with an initial config file
    
    
    m := multiconfig.New()
    
    
    m := multiconfig.NewWithPath("config.toml") // supports TOML, JSON and YAML
    
    
    
    // Get an empty struct for your configuration
    
    
    serverConf := new(Server)
    
    
    
    // Populated the serverConf struct
    
    
    err := m.Load(serverConf) // Check for error
    
    
    m.MustLoad(serverConf) // Panic's if there is any error
    
    
    
    // Access now populated fields
    
    
    serverConf.Port // by default 6060
    
    
    serverConf.Name //"koding"
    
    
    
    

    运行你的应用程序:

    
    
    
    
    # Sets default values first which are defined in each field tag value. 
    
    
    # Starts to load from config.toml
    
    
    $ app
    
    
    
    # Override any config easily with environment variables, environment variables
    
    
    # are automatically generated in the form of STRUCTNAME_FIELDNAME
    
    
    $ SERVER_PORT=4000 SERVER_NAME="koding" app
    
    
    
    # Or pass via flag. Flags are also automatically generated based on the field
    
    
    # name
    
    
    $ app -port 4000 -users "gopher,koding"
    
    
    
    # Print dynamically generated flags and environment variables:
    
    
    $ app -help
    
    
    Usage of app:
    
    
     -enabled=true: Change value of Enabled.
    
    
     -name=Koding: Change value of Name.
    
    
     -port=6060: Change value of Port.
    
    
     -users=[ankara istanbul]: Change value of Users.
    
    
    
    Generated environment variables:
    
    
     SERVER_NAME
    
    
     SERVER_PORT
    
    
     SERVER_ENABLED
    
    
     SERVER_USERS
    
    
    
    

    许可证

    MIT许可证( MIT ) see请参阅许可证详细信息


    Source  Sources