This is a creation in Article, where the information may have evolved or changed.
Today I wrote a go to read the INI file package, has been uploaded to GitHub top, convenient for everyone to use, mainly read the configuration file method. The bottom is a simple document
The official website
Describe
Using Goini, it is easier to read the INI configuration file of Go and various configuration files according to the specific format.
Installation method
gp get github.com/widuu/goini
How to use
INI configuration file grid style column
[database]username = rootpassword = passwordhostname = localhost[admin]username = rootpassword = password[nihao]username = rootpassword = password
Initialization
conf := goini.SetConfig("./conf/conf.ini") //goini.SetConfig(filepath) 其中filepath是你ini 配置文件的所在位置
Get Individual configuration information
username := conf.GetValue("database", "username") //database是你的[section],username是你要获取值的key名称fmt.Println(username) //root
Delete a configuration message
conf.DeleteValue("database", "username") //username 是你删除的keyusername = conf.GetValue("database", "username")if len(username) == 0 { fmt.Println("username is not exists") //this stdout username is not exists}
Add a configuration information
conf.SetValue("database", "username", "widuu")username = conf.GetValue("database", "username")fmt.Println(username) //widuu 添加配置信息如果存在[section]则添加或者修改对应的值,如果不存在则添加section
Get all configuration information
conf.ReadList() //返回[]map[string]map[string]string的格式 即setion=>key->value
Without permission, do not reprint this site any article: Micro network»golang read the INI configuration pkg (read the configuration file)