英文:
Embedding files via relative paths in go
问题
我有这样的文件结构:
lib/
util.go
CHANGELOG.md
在 util.go
中,我需要嵌入 CHANGELOG.md
。我尝试了以下代码:
//go:embed ../CHANGELOG.md
var changelog string
但是我得到了这个错误:pattern ../CHANGELOG.md: invalid pattern syntax
如果我不能通过相对路径嵌入资源,那么在子模块中嵌入资源的最佳实践是什么?
英文:
I have this file structure:
lib/
util.go
CHANGELOG.md
In util.go
I need to embed CHANGELOG.md
. I try:
//go:embed ../CHANGELOG.md
var changelog string
But I get this error: pattern ../CHANGELOG.md: invalid pattern syntax
If I can't embed resources via relative paths, what are the best practices for embedding resources in sub-modules?
答案1
得分: 11
根据嵌入文档的说明:
> 这些模式是相对于包含源文件的包目录进行解释的。路径分隔符是正斜杠,即使在Windows系统上也是如此。模式不能包含'.'、'..'或空路径元素,也不能以斜杠开头或结尾。
这意味着相对路径的根目录是存放Go源文件的文件夹。所以我认为我们不能从其他本地文件夹中嵌入资源。
也许这个评论可以解释为什么设计成这样,而这个答案提供了一个解决方案。
如果有错误,请指导。🎉
英文:
according embed docs:
>The patterns are interpreted relative to the package directory containing the source file. The path separator is a forward slash, even on Windows systems. Patterns may not contain ‘.’ or ‘..’ or empty path elements, nor may they begin or end with a slash.
This means that the root of the relative path is the folder where the go sources files are located. So I think we couldn't embedding resources from other local folders.
Maybe this comment can explain why it's designed like this, and this answer provide a solution.
If there is an error, please advise. 🎉
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论