Beego/config Package Source Analysis

Source: Internet
Author: User
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.goThe 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

datais 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.