go - revel 框架中的相对路径

标签 go revel

如何在 revel 框架中将文件 realtive 导入到 revel basefolder。目前我执行以下操作来获取一些配置值。

file, err := ioutil.ReadFile("conf/config.conf")
...

这导致我的服务器只有在我开始 Revel 时站在应用程序目录中才能工作

revel run myapp

有没有办法访问基本文件夹?

最佳答案

revel中有导出的全局变量包,您可以使用以下任何一种:

var (
    // App details
    AppName    string // e.g. "sample"
    BasePath   string // e.g. "/Users/revel/gocode/src/corp/sample"
    AppPath    string // e.g. "/Users/revel/gocode/src/corp/sample/app"
    ViewsPath  string // e.g. "/Users/revel/gocode/src/corp/sample/app/views"
    ImportPath string // e.g. "corp/sample"
    SourcePath string // e.g. "/Users/revel/gocode/src"

    // Revel installation details
    RevelPath string // e.g. "/Users/revel/gocode/src/revel"

    // Where to look for templates and configuration.
    // Ordered by priority.  (Earlier paths take precedence over later paths.)
    CodePaths     []string
    ConfPaths     []string
    TemplatePaths []string
)

如果它对您来说是空的,那很可能是因为您从基础文件夹启动了您的应用。

请注意,这些路径由 Init(mode, importPath, srcPath string) 设置功能。它的文档说明:

srcPath - the path to the source directory, containing Revel and the app.
  If not specified (""), then a functioning Go installation is required.

另请查看:how to reference a relative file from code and tests

关于go - revel 框架中的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31265072/

相关文章:

interface - Go接口(interface)/容器使用

go - Go 中的持久化程序

selenium - Go Lang 中的自动化 - 如何使用像 Selenium 这样的浏览器自动化?

go - 需要对函数进行类型断言

go - 如何从代码和测试中引用相关文件

go - 直接从 Revel 模板引擎中拉取模型

go - 如何让 main() 等待直到 init() 在 golang 中完成?

html - Golang 在 Revel 中将字符串渲染为 html

git - 在 Heroku 上部署 revel 应用程序

sql - 戈朗 pq : syntax error when executing sql