go项目获取配置文件信息

import (
	"github.com/astaxie/beego"
	//使用了beego框架的配置文件读取模块
	"github.com/astaxie/beego/config"
)

//声明变了
var (
	G_server_name  string //项目名称
	G_server_addr  string //服务器ip地址
	G_server_port  string //服务器端口
	G_redis_addr   string //redis ip地址
	G_redis_port   string //redis port端口
	G_redis_dbnum  string //redis db 编号
	G_mysql_addr   string //mysql ip 地址
	G_mysql_port   string //mysql 端口
	G_mysql_dbname string //mysql db name
	G_fastdfs_port   string //fastdfs 端口
	G_fastdfs_addr string //fastdfs ip
)


func InitConfig() {
	//从配置文件读取配置信息
	appconf, err := config.NewConfig("ini", "/home/itcast/go/src/sss/IhomeWeb/conf/app.conf")
	if err != nil {
		beego.Debug(err)
		return
	}
	//给变量赋值
	G_server_name = appconf.String("appname")
	G_server_addr = appconf.String("httpaddr")
	G_server_port = appconf.String("httpport")
	G_redis_addr = appconf.String("redisaddr")
	G_redis_port = appconf.String("redisport")
	G_redis_dbnum = appconf.String("redisdbnum")
	G_mysql_addr = appconf.String("mysqladdr")
	G_mysql_port = appconf.String("mysqlport")
	G_mysql_dbname = appconf.String("mysqldbname")
	G_fastdfs_port  = appconf.String("fastdfsport")
	G_fastdfs_addr = appconf.String("fastdfsaddr")
	return
}

//项目启动时读取配置文件给变量进行初始化
func init() {
	InitConfig()
}

猜你喜欢

转载自blog.csdn.net/weixin_44282540/article/details/108351865