Bear in mind that accidentally including Mac OS’s. or _, but if you use a wildcard, like dir/*, it will include all files that match, even if they start with. Third, when you include a directory, it won’t include files that start with. bad-level.go:9:4: go:embed cannot apply to var inside func Package main import ( _ "embed" "fmt" ) func main () $ go run bad-level.go go:embed gives us an easy way to include version information from a version.txt file:
In my post on //go:generate, I showed how you could include version information with the Go linker. You can read the official docs for a complete technical explanation, so here let’s take a look at some examples to see what’s possible. The go:embed directive understands Go file globs, so patterns like files/*.html will also work (but not **/*.html recursive globbing).
The comment should look like //go:embed FILENAME(S) and be followed by a variable of the type you want to embed: string or byte for an individual file or embed.FS for a group of files. The basic idea of embedding is that by adding a special comment to your code, Go will know to include a file or files. To demonstrate some of the capabilities of //go:embed, I have made an example repo which I will explain in this post. In addition to the remarkable new flag.Func feature, Go 1.16 also introduced a new //go:embed directive that allows you to include the contents of arbitrary files and directories in your Go application. It exists to “to automate the running of tools to generate source code before compilation.” Now there is a new feature in Go that eliminates many uses of source code generation.
Goland fmt on save how to#
I have previously written about how to use //go:generate. Update: Want to listen to a podcast about go:embed instead of reading a blog post? Check out Go Time episode 171.