Search code examples
goembedded-resource

using ../ at go:embed annotation


I want to embed a file placed one level above the golang file code. for example:

dir1

  • file.go

dir2

  • file.txt

How to embed file.txt inside file.go using go:embed?


Solution

  • The documentation states:

    Patterns may not contain ‘.’ or ‘..’ or empty path elements, nor may they begin or end with a slash.

    So what you are trying to do is not supported directly. Further information is available in the comments on this issue.

    One thing you can do is to put a go file in dir2, embed file.txt in that and then import/use that in dir1/file.go (assuming the folders are in the same package).