华域联盟 Golang GO语言ini配置文件的读取的操作

GO语言ini配置文件的读取的操作

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:[email protected]: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)
 
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持华域联盟。如有错误或未考虑完全的地方,望不吝赐教。

本文由 华域联盟 原创撰写:华域联盟 » GO语言ini配置文件的读取的操作

转载请保留出处和原文链接:https://www.cnhackhy.com/111332.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们