golang 读取配置文件

beego config模块,可以解析文件,目前支持解析的文件格式有 ini、json、xml、yaml

简单例子:

import (
	"fmt"
	"github.com/astaxie/beego/config"
)

func main() {
	//首先初始化一个解析器对象
	conf, err := config.NewConfig("ini", "E:\\golang\\go_work\\src\\go_dev\\day11\\config\\logAgent.conf")
	if err != nil {
		fmt.Println("new config failed, err:", err)
		return
	}

	port, err := conf.Int("server::port")
	if err != nil {
		fmt.Println("read server:port failed, err:", err)
		return
	}

	fmt.Println("Port:", port)
	logLevel := conf.String("logs::log_level")
	if len(logLevel) == 0 {
		logLevel = "debug"
	}

	fmt.Println("log_level:", logLevel)

	logPath := conf.String("logs::log_path")
	fmt.Println("log_path:", logPath)
}

配置文件

[server]
listen_ip = "0.0.0.0"
port = 8080

[logs]
log_level = debug
log_path = ./logs/........log

[collect]
log_path = ........../home/work/logs/
topic = topic01

结果:

Port: 8080
log_level: debug
log_path: ./logs/........log

 

你可能感兴趣的:(Web)