ini包 获取配置文件的包
下载 :go get gopkg.in/ini.v1
文档:[https://github.com/go-ini/ini] [https://ini.unknwon.io/]
##config.ini
[kafka]
address=192.168.42.133:9092
topic=test_log
chan_size=100000
[collect]
logfile_path=g:/logs/s4.log
##main.go
import (
"gopkg.in/ini.v1"
)
type KafkaConfig struct {
Address string `ini:"address"`
Topic string `ini:"topic"`
ChanSize int64 `ini:"chan_size"`
}
type CollectConfig struct {
LogFilePath string `ini:"logfile_path"`
}
type Config struct {
KafkaConfig `ini:"kafka"`
CollectConfig `ini:"collect"`
}
func main(){
var configObj = new(Config)
//0.读配置文件 `go-ini`包
err := ini.MapTo(configObj, "./conf/config.ini")
if err != nil {
log.Error("ini load config failed,err:%v", err)
return
}
kafkaAddr := configObj.KafkaConfig.Address
...
}
来源:CSDN
作者:灵动的叶
链接:https://blog.csdn.net/u013328965/article/details/104797213