Golang无法看到外部包中的模板。

huangapple go评论107阅读模式
英文:

Golang not able to see templates in external package

问题

我正在尝试在Go语言中编写一个可重用的包。我使用的结构与这里描述的类似,但稍有不同:

/src/bitbucket.org/EXTERNAL_PROJECT_NAME/EXTERNAL_PACKAGE_NAME/...
/src/INTERNAL_PROJECT_NAME/INTERNAL_PACKAGE_NAME/...

或者第二行应该是:

/src/bitbucket.org/INTERNAL_PROJECT_NAME/INTERNAL_PACKAGE_NAME/...

一切都正常,直到我需要访问存在于外部包中的非Go文件。例如,我有一些内置模板,我希望它们在不必将它们包含在我的内部项目模板目录中的情况下可用。

为此,我在外部项目中有一个"templates"目录,我希望在其中存放一些内置模板,并在我的内部项目中有一个"templates"目录,用于存放自定义模板。但是,当我尝试从外部项目的模板目录解析模板时,它找不到它们。

那么,我应该如何指示我希望从外部包目录而不是内部包目录获取模板?我可以调整路径,例如:

../../bitbucket.org/EXTERNAL_PROJECT_NAME/EXTERNAL_PACKAGE_NAME/templates/file.html

但这显然非常笨拙,并且依赖于个人设置,所以这种方法行不通。一般来说,如果我想引用外部包中的文件而不是我的内部项目目录中的文件,我应该如何优雅地实现?

谢谢!

英文:

I am attempting to write a reusable package in Go. I'm using a structure similar to that described here but slightly different:

/src/bitbucket.org/EXTERNAL_PROJECT_NAME/EXTERNAL_PACKAGE_NAME/...
/src/INTERNAL_PROJECT_NAME/INTERNAL_PACKAGE_NAME/...

Or should the second line be:

/src/bitbucket.org/INTERNAL_PROJECT_NAME/INTERNAL_PACKAGE_NAME/...

Everything works until I need to access a non-go file that exists in the external package. For example, I have some built in templates that I would like to be available without having to include them in my internal projects templates directory.

To that end, I have a "templates" directory in the external project where I want to house some built-in templates and a "templates" directory in my internal project where custom templates will go. But when I attempt to parse templates from the external project template directory, it can't find them.

So how would I go about indicating that I want to get the templates from the external package directory instead of the internal one? I could adjust the path to something like the following:

../../bitbucket.org/EXTERNAL_PROJECT_NAME/EXTERNAL_PACKAGE_NAME/templates/file.html

but this is obviously very clumsy and depends on individual setup, so that's not going to work. In general, if I want to reference a file in an external package instead of my internal project directory, how would I do this gracefully?

Thanks!

答案1

得分: 2

看起来有一个相当简单的解决方案。代码如下所示:

package main

import (
    "bitbucket.org/EXTERNAL_PROJECT/EXTERNAL_PACKAGE"
    "go/build"
)

func main() {
    SrcRoot := "/src"
    PackageDir := "/bitbucket.org/EXTERNAL_PROJECT/EXTERNAL_PACKAGE"
    InternalTemplateDir := build.Default.GOPATH + SrcRoot + PackageDir + "/templates/"
}

这里的GOROOT提供了包含所有Go代码的目录路径。从那里,我想引用包源代码中的模板目录。通过InternalTemplateDir,我现在有了一个基本路径,可以在外部包中引用模板。

为了方便使用,我可能会构建一个模板加载器,首先检查内部文件路径上是否存在文件,然后再检查外部包中是否存在相同的文件,这样任何给定的模板都可以通过在内部包含它来进行覆盖,但是必要的模板也将具有内置版本。

英文:

Turns out there is a pretty simple solution. Looks something like the following:

package main

import (
	"bitbucket.org/EXTERNAL_PROJECT/EXTERNAL_PACKAGE"
    "go/build"
)

func main() {
    SrcRoot := "/src"
    PackageDir := "/bitbucket.org/EXTERNAL_PROJECT/EXTERNAL_PACKAGE"
    InternalTemplateDir := build.Default.GOPATH + SrcRoot + PackageDir + "/templates/"
}

GOROOT here provides us with the path to the directory containing all our go code. From there, I want to reference the templates directory in the package source. With InternalTemplateDir, I now have the base path from which to reference templates within the external package.

For ease of use, I will probably build a template loader that checks for a file on an internal file path first and then checks for the same file in the external package, so that any given template can be overridden by including it internally, but essential templates will all have built in versions as well.

答案2

得分: 0

如果不是Go包(即bitbucket.org/EXTERNAL_PROJECT_NAME/EXTERNAL_PACKAGE_NAME/file.go),它将无法工作,你最好使用类似https://github.com/jteeuwen/go-bindata的方法。

但我真的认为你应该重新考虑你的问题,并采用不同的方法来解决。

英文:

If it's not a Go package (aka bitbucket.org/EXTERNAL_PROJECT_NAME/EXTERNAL_PACKAGE_NAME/file.go) it's not gonna work, your best bet us something like https://github.com/jteeuwen/go-bindata.

But I really think you should rethink your problem and use a different approach to it.

huangapple
  • 本文由 发表于 2014年8月2日 02:30:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/25086489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定