A configuration loader in Go

235 views
Skip to first unread message

zsou...@gmail.com

unread,
May 23, 2017, 12:48:41 PM5/23/17
to Golang-China

goconf

Overview

  • Read configuration automatically based on the given struct's field name.
  • Load configuration from multiple sources
  • multiple file inherit

Values are resolved with the following priorities (lowest to highest):

  1. Options struct default value
  2. Flags default value
  3. Config file value, TOML or JSON file
  4. Command line flag

About field tags in structs

type TestOptions struct {
    Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"`
}
  • flag is the name passed from the command line.
  • cfg is the name used in config files.
  • default is the default value

If do not define flag tag, flag will be snake case of the fild name.

If do not define cfg tag, cfg value will be flag value.

For example, the following example, flag will be http_address, cfg will be http_address.

  HTTPAddress string

Usage

load multiple config files

package main

import "github.com/zsounder/goconf"

type TestOptions struct {
    goconf.AutoOptions
    HTTPAddress string `default:"0.0.0.0:0000"`
    Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"`
    LogLevel int `default:"3"`
    BoolVar bool `default:"false"`
}

func main() {
   ops := &TestOptions{}
   goconf.ResolveAutoFlag(ops,"conf_1.toml","conf_2.toml")
}

go run main.go --log_level=1

The output will be:

[Config] auto flag succ, name: _auto_conf_files_ val:
[Config] auto flag succ, name: _auto_dir_running_ val:
[Config] auto flag succ, name: http_address val: 0.0.0.0:0000
[Config] auto flag fail, name: hosts val: 127.0.0.0,127.0.0.1 err: type not support []string
[Config] auto flag succ, name: log_level val: 3
[Config] auto flag succ, name: bool_var val: false
[Config] file: [conf_1.toml conf_2.toml]
[Config] load: conf_1.toml
[Config] load: conf_2.toml
[Config]
{
   "AutoConfFiles": "",
   "AutoDirRunning": "",
   "HTTPAddress": "127.0.0.1:2",
   "Hosts": [
      "10.0.61.29",
      "10.0.61.30",
      "10.0.61.31",
      "10.0.61.32"
   ],
   "LogLevel": 1,
   "BoolVar": true
}

load config file with file inherited

package main

import "github.com/zsounder/goconf"

type TestOptions struct {
    goconf.AutoOptions
    HTTPAddress string `default:"0.0.0.0:0000"`
    Hosts []string `flag:"hosts" cfg:"hosts" default:"127.0.0.0,127.0.0.1"`
    LogLevel int `default:"3"`
    BoolVar bool `default:"false"`
}

func main() {
   ops := &TestOptions{}
   // conf_3 inherit from conf_1 and conf_2
   goconf.ResolveAutoFlag(ops,"conf_3.toml")
}

go run main.go --http_address=0.0.0.0:1111111

The output will be:

[Config] auto flag succ, name: _auto_conf_files_ val:
[Config] auto flag succ, name: _auto_dir_running_ val:
[Config] auto flag succ, name: http_address val: 0.0.0.0:0000
[Config] auto flag fail, name: hosts val: 127.0.0.0,127.0.0.1 err: type not support []string
[Config] auto flag succ, name: log_level val: 3
[Config] auto flag succ, name: bool_var val: false
[Config] file: [conf_3.toml]
[Config] load: ./conf_1.toml
[Config] load: ./conf_2.toml
[Config] load: conf_3.toml
[Config]
{
   "AutoConfFiles": "",
   "AutoDirRunning": "",
   "HTTPAddress": "0.0.0.0:1111111",
   "Hosts": [
      "10.0.61.29",
      "10.0.61.30",
      "10.0.61.31",
      "10.0.61.32",
      "10.0.61.33"
   ],
   "LogLevel": 2,
   "BoolVar": true
}

Hyacinthus

unread,
May 23, 2017, 9:51:31 PM5/23/17
to Golang-China
这种配置库超多,但是star都不多。

现在微服务和容器当道,go又作为最适合微服务的语言,根据最佳实践,配置是放在环境变量里的。你可以重点支持一下环境变量。

我以前用的 koding/multiconfig
tj大神还写过一个 不过不支持 default 用起来代码长 没用
从flag为重点 大家用的是 viper 吧

<zsou...@gmail.com>于2017年5月24日周三 上午12:48写道:
--
--
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina
---
您收到此邮件是因为您订阅了Google网上论坛上的“Golang-China”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到golang-china...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/golang-china/41ad3ea7-9a8d-41b2-968c-a2adaa54efcc%40googlegroups.com
要查看更多选项,请访问https://groups.google.com/d/optout

zsou...@gmail.com

unread,
Sep 30, 2018, 5:32:05 AM9/30/18
to Golang-China
的确是这样,这个repo也是在自己线上项目一直在用的,没有把flag支持加强,主要是用的配置文件比较多,环境比较多,不同文件之前还要做依赖,继承,所以就有了这么个东西。

Muninn於 2017年5月24日星期三 UTC+8上午9時51分31秒寫道:
<zsou...@gmail.com>于2017年5月24日周三 上午12:48写道:
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到golang-china+unsubscribe@googlegroups.com
Reply all
Reply to author
Forward
0 new messages