beego中解析配置文件的代码在github.com/astaxie/beego/config包内,初始化文件是github.com/astaxie/beego/config.go。beego提供了两种类型配置的解析:ini和json,默认解析ini类型的配置文件。Register方法注册解析方法,我们叫它adapter,Register被调用的位置是adapter的init函数,也就是说adapter通过包引用自动注册。
- Register方法位置:github.com\astaxie\beego\config\config.go
// Register makes a config adapter available by the adapter name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, adapter Config) {
if adapter == nil {
panic("config: Register adapter is nil")
}
if _, ok := adapters[name]; ok {
panic("config: Register called twice for adapter " + name)
}
adapters[name] = adapter
}
接着看一下adapter ini,
- 文件位置:github.com\astaxie\beego\config\ini.go
func init() {
Register("ini", &IniConfig{})
}
初始化函数很简单,只是调用了Register方法把自己注册到adapters中,IniConfig继承了Config接口(在config\config.go中定义)
// Config is the adapter interface for parsing config file to get raw data to Configer.
type Config interface {
Parse(key string) (Configer, error)
ParseData(data []byte) (Configer, error)
}
接口方法的实现在config\ini.go文件中
// Parse creates a new Config and parses the file configuration from the named file.
func (ini *IniConfig) Parse(name string) (Configer, error) {
return ini.parseFile(name)
}
func (ini *IniConfig) parseFile(name string) (*IniCo