Skip to content
Latchkey

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.

go build output
./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 string

Common 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.

Go
import "embed"

//go:embed templates/*.html
var templates embed.FS

Use string or []byte for a single file

Go
//go:embed version.txt
var version string

How to prevent it

  • Match the embed target type to what you embed: scalar for one file, embed.FS for many.
  • Import embed whenever you use embed.FS.
  • Use embed.FS for any directory or glob that can match multiple files.

Frequently asked questions

What causes ""cannot apply to var of type""?
//go:embed requires string, []byte, or embed.FS. Any other type (an int, a struct, a map) is rejected.
How do I fix "cannot apply to var of type"?
Bind a directory or multi-file pattern to an embed.FS.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card