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?
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. š
you can add a embed.go file
embed.go
package project-root
import "embed"
//go:embed CHANGELOG.md
var Changelog string
util.go
func main() {
log := project-root.Changelog
}
Project Structure
project-root/
āāā lib/
ā āāā util.go
āāā CHANGELOG.md
āāā embed.go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With