GO语言ini配置文件的读取的操作
作者:Coldestmonth 时间:2024-04-23 09:38:36
iniconf
博主前两天在写一个小的go语言项目,想找一个读取ini格式配置和的三方库,在网上找了一圈感觉都不是很好用, 使用起来非常的奇怪,于是自己写了一版,还有两个接口没有实现,在项目中修改或删除配置项后更新到文件中,等待后续有时间了再写,希望用的朋友感觉不错点个赞
github 地址
描述
使用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
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
补充:GoLang 使用goconfig读取配置文件(.ini、.json、.yaml)
一、goconfig读取.ini类型配置文件
1、配置文件(config.ini)如下:
[RabbitMQ]
MQUrl = amqp://trkj:trkj@192.168.5.62:5672/test
Exchange = EX.WALLDATA
ExchangeType = fanout
RoutingKey = RK.WALLDATA
[Base]
messageFrequency = 5
2、解析配置文件:
package main
import (
"fmt"
"github.com/hyahm/goconfig"
)
type RabbitMQ struct {
MQUrl string
Exchange string
ExchangeType string
RoutingKey string
}
type BaseConfig struct {
MsgFrequency int64 // 消息发送频率
RabbitMQ // MQ信息
}
func ReadBaseConfig(bconfig *BaseConfig, confFile string) {
goconfig.InitConf(confFile, goconfig.INI)
bconfig.MsgFrequency = goconfig.ReadInt64("Base.messageFrequency", 3)
bconfig.RabbitMQ.MQUrl = goconfig.ReadString("RabbitMQ.MQUrl", "")
bconfig.RabbitMQ.Exchange = goconfig.ReadString("RabbitMQ.Exchange", "")
bconfig.RabbitMQ.ExchangeType = goconfig.ReadString("RabbitMQ.ExchangeType", "")
bconfig.RabbitMQ.RoutingKey = goconfig.ReadString("RabbitMQ.RoutingKey", "")
}
func main() {
baseConfig := BaseConfig{}
ReadBaseConfig(&baseConfig, "./Config.ini")
fmt.Printf("mq.MQUrl = %s \t mq.Exchange = %s \t mq.ExchangeType = %s \t mq.RoutingKey = %s\n", baseConfig.RabbitMQ.MQUrl, baseConfig.RabbitMQ.Exchange, baseConfig.RabbitMQ.ExchangeType, baseConfig.RabbitMQ.RoutingKey)
fmt.Printf("msgFrequency = %d\n", baseConfig.MsgFrequency)
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持asp之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/len_yue_mo_fu/article/details/84701379
标签:GO,ini,配置文件


猜你喜欢
python中的__slots__使用示例
2022-05-09 17:50:56
Python requests的SSL证书验证方式
2021-10-06 09:21:51

浅谈js对象属性 通过点(.) 和方括号([]) 的不同之处
2024-04-22 13:01:00
mysql慢查询日志轮转_MySQL慢查询日志实操
2024-01-26 05:54:14

Python locust工具使用详解
2021-11-04 04:04:56

如何使用Python生成Hilbert矩阵
2021-08-09 12:36:08

python读取xml文件方法解析
2021-04-25 03:53:45
微信小程序实现tab点击切换
2024-04-18 09:49:22

python实现马耳可夫链算法实例分析
2022-08-13 00:27:08
由浅入深讲解python中的yield与generator
2022-08-14 06:26:11
Django实现单用户登录的方法示例
2021-12-21 03:26:41

Windows下PyCharm2018.3.2 安装教程(图文详解)
2022-07-09 12:05:53

PHP 正则判断中文UTF-8或GBK的思路及具体实现
2024-06-05 09:49:00
在PyCharm环境中使用Jupyter Notebook的两种方法总结
2021-10-07 08:06:32

python序列类型种类详解
2022-02-27 13:53:38
Python 异常处理Ⅳ过程图解
2023-06-28 16:05:53

keras实现VGG16 CIFAR10数据集方式
2023-08-19 08:27:08

Python中正反斜杠(‘/’和‘\\’)的意义与用法
2023-01-22 14:45:04
css实现图片倒影效果
2007-11-05 18:29:00

Python新建项目自动添加介绍和utf-8编码的方法
2023-02-07 07:58:06
