Go "go:embed cannot apply to var of type" - Fix Embed Targets in CI
//go:embed may only target a string, a []byte, or an embed.FS. Pointing it at any other type - or embedding multiple files into a scalar string/[]byte - is a compile error, because the directive and the variable type must agree.
What this error means
A build fails with go:embed cannot apply to var of type <T> or invalid go:embed: multiple files for type string. The pattern matches files, but the variable it binds to is the wrong type for what was embedded.
./assets.go:9:5: go:embed cannot apply to var of type int
# or, many files into a scalar:
./assets.go:9:5: invalid go:embed: multiple files matched but type is stringCommon causes
The target var is not an embeddable type
//go:embed requires string, []byte, or embed.FS. Any other type (an int, a struct, a map) is rejected.
Multiple files embedded into a scalar
A string or []byte can hold exactly one file. A pattern that matches several files (or a directory) must target an embed.FS.
How to fix it
Use embed.FS for multiple files
Bind a directory or multi-file pattern to an embed.FS.
import "embed"
//go:embed templates/*.html
var templates embed.FSUse string or []byte for a single file
//go:embed version.txt
var version stringHow to prevent it
- Match the embed target type to what you embed: scalar for one file,
embed.FSfor many. - Import
embedwhenever you useembed.FS. - Use
embed.FSfor any directory or glob that can match multiple files.