解析ini配置文件

1.解析ini配置文件

config.ini

[kafka]
address=127.0.0.1:9092
topic=web_log

[logtail]
path=./my.log

对应的结构体

package conf

type KafkaConfig struct {
	Address []string `ini:"address"`
	Topic   string   `ini:"topic"`
}

type LogtailConfig struct {
	Path string `ini:"path"`
}

type AppConfig struct {
	KafkaConfig   `ini:"kafka"`
	LogtailConfig `ini:"logtail"`
}

依赖的第三方包
"gopkg.in/ini.v1"

使用

package main

import (
	"fmt"
	"logAgent/conf"
	"time"

	"gopkg.in/ini.v1"
)

var (
	config = new(conf.AppConfig)
)

func main() {
	fmt.Printf("Start Agent...now time:%#v\n", time.Now().Format("2006-01-02 15:04:05"))
	err := ini.MapTo(config, "./conf/config.ini")
	if err != nil {
		fmt.Printf("parse init config failed,%#v\n", err)
		return
	}
	
}

posted @   SpecialSpeculator  阅读(211)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示