解析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 @ 2022-05-25 22:11  SpecialSpeculator  阅读(211)  评论(0编辑  收藏  举报