Go "cannot use X (variable of type A) as B value" - Fix in CI
By Daniel Zoghalchali·Latchkey
Go is statically typed and never implicitly converts between types. Passing a value whose type does not match the parameter is a compile error.
What this error means
A build fails with cannot use x (variable of type A) as B value in argument to f. It commonly follows a signature change or a refactor that altered a value type.
go
./api.go:21:14: cannot use id (variable of type int64) as string value in argument to lookup
Diagnose it: toolchain, tags, or platform?
Go build failures that only appear in CI usually come from a different toolchain version, different build tags, or cross-compilation defaults that differ from your machine.
Terminal
go version
go env GOOS GOARCH CGO_ENABLED GOFLAGS GOTOOLCHAIN
# build exactly what CI builds, verbosely
go build -v ./... 2>&1 | tail -40
# CGO is the usual difference: on by default locally, often off in a slim CI image
CGO_ENABLED=0 go build ./...
Common causes
Signature changed
A parameter type changed but a caller still passes the old type.
Missing explicit conversion
Go requires an explicit conversion between numeric or named types; the call omitted it.
How to fix it
Convert the value explicitly
Wrap the argument in the target conversion, or fix the caller to pass the right type.
Go
lookup(strconv.FormatInt(id, 10))
Align callers with the signature
Update every call site after changing a function signature, then rebuild.
Terminal
go build ./...
How to prevent it
Update all call sites when you change a signature.
Build locally before pushing to catch type mismatches.
Prefer explicit conversions over relying on inference.
Frequently asked questions
What causes Go "cannot use X (variable of type A) as B value"?
There are 2 common causes: signature changed and missing explicit conversion. A parameter type changed but a caller still passes the old type.
How do I fix Go "cannot use X (variable of type A) as B value"?
There are 2 fixes depending on which cause you have: convert the value explicitly and align callers with the signature. Work through them in order, since the first is the most common.
What does Go "cannot use X (variable of type A) as B value" actually mean?
A build fails with cannot use x (variable of type A) as B value in argument to f.
How do I stop Go "cannot use X (variable of type A) as B value" happening again?
Update all call sites when you change a signature. The prevention section lists 3 changes that keep it from recurring.