This is a creation in Article, where the information may have evolved or changed.
Since the end of December last year, contacts with Golang have been used in the Revel and beego frameworks. Finally chose the Beego, uses the beego reason is also very simple, because Beego is the Chinese development, has the Chinese document.
Golang originates from the C language, although object-oriented programming is not supported, it provides interfaces interface, anonymous fields, and so on. While object-oriented programming is a bit awkward, it can be done at the very least. Here Jiehuaxianfo, introduce Beego config package, by the way Golang object-oriented programming.
The Config package config.go
file defines two interfaces, ConfigContainer
and Config
Configcontainer defines how to get configuration information from the original data. The config interface is an adapter that defines a method for parsing raw data to Configcontainer.
The other files in the Config package are implementations of these two interfaces. XML folder implements the method of parsing XML data format, Yaml folder implements the method of parsing Yaml format, INI and JSON parsing INI format and JSON format respectively. The main point here is the implementation of the INI format.
ini.go
The file defines two structures IniConfigContainer
and IniConfig
, respectively, implements the above two interfaces. The implementation syntax of the Golang interface can refer to the previous article "Golang Interface Implementation". The Iniconfigcontainer not only implements the interface, but also defines the internal variables to assist the implementation.
filename stringdata map[string]map[string]string // section=> key:valsectionComment map[string]string // section : commentkeyComment map[string]string // id: []{comment, key...}; id 1 is for main comment.sync.RWMutex
data
is a map implementation of the key-value pairs that are used to save the configuration file. Iniconfigcontainer also has an anonymous object sync.RWMutex
, which is the Golang inherited syntax, stating that Iniconfigcontainer inherits the synchronization lock, where it is used for mutexes to modify the value of the configuration file. The implementation of config package and INI format can be referenced.