Home > OS >  Embedding files via relative paths in go
Embedding files via relative paths in go

Time:11-24

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?

CodePudding user response:

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.

  •  Tags:  
  • go
  • Related