revel框架的配置文件管理
作者:网络转载 发布时间:[ 2015/5/7 10:58:59 ] 推荐标签:软件测试管理

revel中的配置管理使用了https://github.com/robfig/config 包,该包支持的格式和.ini格式有些类似,支持分节,同时文件内变量还可以相互引用。一个典型的配置:
[DEFAULT]
host: www.example.com
protocol: http://
base-url: %(protocol)s%(host)s
[service-1]
url: %(base-url)s/some/path
delegation: on
maxclients: 200 # do not set this higher
comments: This is a multi-line
entry # And this is a comment
可以看到,在变量外加上%( )s可以引起变量的值。方括号可以定义不同的分节。
读取上面的配置文件:
c, _ := config.ReadDefault("config.cfg")
c.String("service-1", "url")
// result is string "http://www.example.com/some/path"
c.Int("service-1", "maxclients")
// result is int 200
c.Bool("service-1", "delegation")
// result is bool true
c.String("service-1", "comments")
在revel中,针对开发环境和生产环境使用了不同的分节来配置:
[dev]
mode.dev = true
watch = true
[prod]
mode.dev = false
watch = false
切换环境时,取不同的分节的值,可以实现不同的配置了。

sales@spasvo.com