使用iniconf更简单的读取go的ini配置文件以及根据特定格式的各种配置文件。
go get github.com/clod-moon/goconf
ini配置文件格式样列
[database]
username = root
password = password
hostname = localhost
[admin]
username = root
password = password
[nihao]
username = root
password = password
初始化
conf := goini.InitConfig("./conf/conf.ini") //iniconf.SetConfig(filepath) 其中filepath是你ini 配置文件的所在位置
获取单个配置信息
username := conf.GetValue("database", "username") //database是你的[section],username是你要获取值的key名称
fmt.Println(username) //root
删除一个配置信息
conf.DeleteValue("database", "username") //username 是你删除的key
username = conf.GetValue("database", "username")
if len(username) == 0 {
fmt.Println("username is not exists") //this stdout username is not exists
}
添加一个配置信息
conf.SetValue("database", "username", "chun")
username = conf.GetValue("database", "username")
fmt.Println(username) //chun 添加配置信息如果存在[section]则添加或者修改对应的值,如果不存在则添加section
获取所有配置信息
conf.GetAllSetion() //返回map[string]map[string]string的格式 即setion=>key->value
使用iniconf更简单的读取go的ini配置文件以及根据特定格式的各种配置文件。
<<<<<<< HEAD
go get github.com/clod-moon/goconf
conf.ini
[database]
username = root
password = password
hostname = localhost
[admin]
username = root
password = password
[nihao]
username = root
password = password
initialize
conf := goini.InitConfig("./conf/conf.ini") //goini.InitConfig(filepath) filepath = directory+file
To obtain a single configuration information
username := conf.GetValue("database", "username") //username is your key you want get the value
fmt.Println(username) //root
To delete a configuration information
conf.DeleteValue("database", "username") //username is your delete the key
username = 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", "chun")
username = conf.GetValue("database", "username")
fmt.Println(username) //chun Adding/section configuration information if there is to add or modify the value of the corresponding, if there is no add section
Get all the configuration information
conf.GetAllSetion() //return map[string]map[string]string example:setion=>key->value
func main() {
conf := iniconf.InitConfig("./config.ini")
for key,value :=range conf.Conflist {
fmt.Println(key)
for k,v := range value{
fmt.Println(k,":",v)
}
}
fmt.Println(conf.GetValue("esinfo","addr"))
conf.SetValue("esinfo","addr","127.100.100.100")
fmt.Println(conf.GetValue("esinfo","addr"))
}
output
esinfo
addr : 127.0.0.1
port : 9200
index : case
type : case
127.0.0.1
127.100.100.100
Process finished with exit code 0