偶然在看一个ini读写的go框架的源代码看到如下代码:

type Config struct {
	filepath string                         //your ini file path directory+file
	conflist []map[string]map[string]string //configuration information slice
}

突然就纳闷了[]map[string]map[string]string这个到底是啥。

查看了其他的关于这个conflist的使用,比如从中读取一个通过selection和key值获取value的方法,遂理解。

//To obtain corresponding value of the key values
func (c *Config) GetValue(section, name string) string {
	c.ReadList()
	conf := c.ReadList()
	for _, v := range conf {
		for key, value := range v {
			if key == section {
				return value[name]
			}
		}
	}
	return "no value"
}

首先[]map[string]map[string]string可以拆开来看。

第一个空的[]表示数组,是一个map符合叠加的数组。

其次是map[xxxx] xxxx的用法,括号内是key,括号外的是value,因此map[string]map[string]string定位为:

map[key1] map[key2] value2

也就是

map[selection] map[key] value

这样也符合ini文件的特点,selection表示一块的值,其底下含有各key:value的格式,使用数组表示多组,ini文件的多组selection。

分类: Go

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注