golang/go · error
unknown compiler %q
Error message
unknown compiler %q
What it means
buildCompiler.Set is the flag.Value for -compiler. It accepts only 'gc' (the standard Go compiler) and 'gccgo' (the GCC-based Go frontend). Any other value returns this error, and flag parsing aborts before any build work begins.
Source
Thrown at src/cmd/go/internal/work/build.go:283
var (
BuildToolchain toolchain = noToolchain{}
ldBuildmode string
)
// buildCompiler implements flag.Var.
// It implements Set by updating both
// BuildToolchain and buildContext.Compiler.
type buildCompiler struct{}
func (c buildCompiler) Set(value string) error {
switch value {
case "gc":
BuildToolchain = gcToolchain{}
case "gccgo":
BuildToolchain = gccgoToolchain{}
default:
return fmt.Errorf("unknown compiler %q", value)
}
cfg.BuildToolchainName = value
cfg.BuildContext.Compiler = value
return nil
}
func (c buildCompiler) String() string {
return cfg.BuildContext.Compiler
}
func init() {
switch build.Default.Compiler {
case "gc", "gccgo":
buildCompiler{}.Set(build.Default.Compiler)
}
}
type BuildFlagMask intView on GitHub (pinned to b6b368adc5)
Solutions
- Use '-compiler=gccgo' for the GCC-based Go frontend (install gccgo first).
- Omit -compiler entirely to use the default 'gc'.
- Check GOFLAGS in the environment: 'go env GOFLAGS' and unset the bad value.
- For C compiler selection in cgo, use CC= not -compiler.
Example fix
// before $ go build -compiler=gcc ./... // error: unknown compiler "gcc" // after $ go build -compiler=gccgo ./... # if you meant gccgo # or $ go build ./... # default gc compiler # for cgo C-compiler selection: $ CC=gcc go build ./...
Defensive patterns
Strategy: validation
Validate before calling
var validCompilers = map[string]bool{"gc": true, "gccgo": true}
func validateCompilerFlag(v string) error {
if !validCompilers[v] {
return fmt.Errorf("-compiler must be 'gc' or 'gccgo', got %q", v)
}
return nil
} Prevention
- Always cross-check -compiler values against the allowed set before invoking go.
- Audit GOFLAGS in CI for unknown compiler names.
- Use CC= for selecting the C compiler in cgo, not -compiler.
When it happens
Trigger: Invoking 'go build -compiler=<x>' (or GOFLAGS=-compiler=<x>, or setting the compiler via -gcflags with a bad toolchain name) where <x> is not 'gc' or 'gccgo'. Commonly a typo like 'gcc' or 'llvm' or 'go'.
Common situations: Users coming from C ecosystems who try '-compiler=gcc'; tooling scripts that interpolate an unknown compiler name; copied GOFLAGS from another project.
Related errors
- failed to locate cmd/compile for target platform
- CC not set and no default found
- local imports disallowed
- file not found
- cannot import "main"
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/2abfb9b86424dd38.
Report an issue: GitHub.