github.com/astaxie/beego/config 下的配置文件读取

83 阅读1分钟

1.配置文件

项目下有一个conf文件夹,conf文件夹下有一个app.conf文件

udPaddr=192.168.40.111
UdpPort=8080
[server]
listenIp = "0.0.0.0"
listenPort = 8888
[logs]
logLevel=debug
logPath=./logs/logagent.log
[collect]
collectPath=D:\project\logs\logagent.log

2.文件读取

package main

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

func init() {
   logs.EnableFuncCallDepth(true)
   logs.SetLogFuncCallDepth(3)
   _ = logs.SetLogger("console")
}
func main() {
   conf, err := config.NewConfig("ini", "conf/app.conf")
   if err != nil {
      logs.Info("配置文件适配出错, err:", err)
   }

   //想把读配置文件读成什么类型,关键看conf.后面调用的啥方法
   udPort1, _ := conf.Int("UdpPort")
   UdpPort2 := conf.String("UdpPort")
   logs.Info("UDPport:", udPort1, UdpPort2)

   listenPort, err := conf.Int("server::listenPort")
   if err != nil {
      logs.Info("server发送 字段读取出错, err:", err)
   } else {
      logs.Info("listenPort:", listenPort)
   }

   logLevel := conf.String("logs::logLevel")
   logs.Info("logLevel:", logLevel)

   logPath := conf.String("collect::logPath")
   logs.Info("logPath:", logPath) //linux系统的路径读不出来

   collectPath := conf.String("collect::collectPath")
   logs.Info("collectPath:", collectPath) //win系统的路径可读出来

}