Go "missing return at end of function" - Fix in CI
A function declared to return a value has a path that reaches its end without a return. Go proves this statically and rejects it, because a function with a return type must return on every path.
What this error means
The build stops with missing return at end of function, pointing at the closing brace. The function compiles fine if every branch returns, but a fall-through path (often after a switch or if/else) leaves Go unable to guarantee a return.
./classify.go:18:1: missing return at end of functionCommon causes
A switch or if chain without a final return
When the compiler cannot prove the branches are exhaustive (no default, or a non-terminating loop), the path after them can reach the end with no return.
A loop the compiler cannot prove is infinite
A for loop that returns inside but is not an unconditional for {} leaves a theoretical exit path with no return after it.
How to fix it
Add a terminal return or default
Give every path a return - a final return, a default that returns, or a panic for the unreachable case.
func classify(n int) string {
switch {
case n > 0:
return "pos"
case n < 0:
return "neg"
default:
return "zero"
}
}Panic on a truly unreachable path
When a path should never happen, make the intent explicit instead of returning a dummy value.
panic("unreachable")How to prevent it
- Make switch statements exhaustive or add a
default. - End value-returning functions with a definite return or panic.
- Run
go build ./...andgo vet ./...before merging.