使用Beego框架开发后端-3.路径统一

数据的路径问题可以使用一个xml文件统一管理

比如说我需要统一将数据放在一个路径下,models里面写一个getPath函数就可以了

在conf下写个xml文件如下,为我的数据库路径

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <path>/Users/duxiaofeng/Desktop/database/</path>
</config>

 再写一个getPath函数,得到该路径

package models

import (
	"encoding/xml"
	"io/ioutil"
	// "fmt"
)

type PathFile struct {
	Path string `xml:"path"`
}

func GetPath() string {
	pathFile, err := ioutil.ReadFile("./conf/path.xml")
	data := PathFile{}
	err = xml.Unmarshal(pathFile, &data)
	if err != nil {
		panic(err)
	}
	return data.Path
}

返回结果是

/Users/duxiaofeng/Desktop/database/

那么文件路径就统一了。

猜你喜欢

转载自blog.csdn.net/reigns_/article/details/89367396