iniconf
博主前两天在写一个小的go语言项目,想找一个读取ini格式配置和的三方库,在网上找了一圈感觉都不是很好用, 使用起来非常的奇怪,于是自己写了一版,还有两个接口没有实现,在项目中修改或删除配置项后更新到文件中,等待后续有时间了再写,希望用的朋友感觉不错点个赞
描述
使用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.InitConfig(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
About
使用iniconf更简单的读取go的ini配置文件以及根据特定格式的各种配置文件。
example
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
扫描二维码关注公众号,回复: 4473018 查看本文章
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