Fix Go "missing return at end of function" — Return Paths, Named Results, and Branch Coverage
A function that promises a result has to deliver one on every path through its body, not just on the paths the author was thinking about when the return was written. The compiler checks the shape of the body, not the values that will flow through it at run time: a return inside an if branch, a switch case, or a loop body covers only the paths that reach that statement, and every other path still falls off the end of the function. Each section below takes one shape, shows the exact message it prints, and gives the fix that belongs to that shape.
Written November 19, 2026 · Tested with Go 1.26.4 · 24 min read
- The first error: a function with result parameters and no return at all
- Reading the message: the position is the closing brace
- If/else: one branch returns, the other falls through
- Switch: returns in the cases, no return after the switch
What this guide is
missing return is the compiler reporting a path, not a value. A function whose signature declares result parameters — func double(n int) int, with int after the parameter list — has promised the caller a value of that type on every call. If any path through the function body can reach the closing brace without executing a return, the promise is broken for that path, and the program does not compile. The compiler does not ask whether the path is likely, whether the loop always runs once, or whether the missing case can happen in practice. It asks only whether the body's statement list ends in a terminating statement, a term the specification defines precisely: a return or goto statement, a call to panic, a block whose statement list ends in a terminating statement, an if with an else branch where both branches are terminating, a for with no break, no loop condition, and no range clause, a switch with no break, a default case, and every case ending in a terminating statement, and a few similar forms. Anything else is not terminating, and a function with result parameters whose body does not end in one is invalid.
One wording point first, because the title and the compiler do not use the same words. The phrase "missing return at end of function" is how the defect is described — in the specification's own example, a function like this is annotated // invalid: missing return statement — but the message Go 1.26.4 actually prints is the shorter missing return. There is no "at end of function" in the output, and no second line naming the path that falls through. The position carries the rest of the information: it points at the function's closing brace, the place where control would fall off the end.
The examples here use Go 1.26.4. Every example lives in a single file named main.go inside one module, created once from an empty directory:
$ mkdir missingreturn
$ cd missingreturn
$ go mod init example.com/missingreturn
go: creating new go.mod: module example.com/missingreturn
Each program that follows replaces the contents of that main.go. When a build fails, the compiler prints the module path as a header line above the error — # example.com/missingreturn here.
The first error: a function with result parameters and no return at all
The smallest program that earns the message declares a result and then never returns one:
// main.go
package main
import "fmt"
func double(n int) int {
fmt.Println(n)
}
func main() {
fmt.Println(double(3))
}
$ go build
# example.com/missingreturn
./main.go:7:1: missing return
Reading the message: the position is the closing brace
The position, ./main.go:7:1, is the closing brace of double — line 7, column 1 — not the line where a return should go, and not the line of the statement that failed to return. There is no such line: the defect is the absence of a statement, and the compiler points at the place where the absence becomes final, the end of the function body. Every case in this guide prints its position in the same place, the closing brace of the function that can fall through, so the line number identifies which function is at fault and nothing more. Which path inside that function is the open one has to be found by reading the body, path by path, and that is what the sections below do.
The other thing the message does not say is which value is missing. It does not name int, or double, or the call in main. The signature already states the type, and the position already states the function; the message adds only that no return stands at the end of it. The fix for this first shape is the direct one: end the function with a return of the declared type — return n * 2 in place of the print, or after it — so the body's statement list ends in a terminating statement.
Note what the error is not. It is not a complaint that a returned value has the wrong type, or that a call passes the wrong number of arguments. Those are checked once a return exists. It is also not the unused-variable error, and adding a variable does not help: a function can compute the right value, hold it in a local variable, print it, and still fail, because computing a value is not returning it. Go has no implicit return. The last statement of a function is not its result, whatever it computes, and the value a function computed but never returned is discarded with the call frame, not handed to the caller.
If/else: one branch returns, the other falls through
A return in one branch of an if covers only that branch. In the program below, the n >= 0 branch returns, and the else branch prints a message and then ends, so a negative input reaches the closing brace with no value returned:
// main.go
package main
import "fmt"
func sign(n int) int {
if n >= 0 {
return 1
} else {
fmt.Println("negative")
}
}
func main() {
fmt.Println(sign(-2))
}
$ go build
# example.com/missingreturn
./main.go:11:1: missing return
The position is again the closing brace, line 11. The compiler is not confused about the if: it knows the first branch returns. It is reporting the second branch, which does not, and the fact that the if as a whole therefore does not end in a terminating statement. The specification's rule for an if is exact on this point: an if statement is terminating only when the else branch is present and both branches are terminating statements. One returning branch and one printing branch is not terminating, however obvious it feels that the two branches cover every input between them.
There are two repairs, and they are not interchangeable in style, though both satisfy the compiler. The first is to make the second branch return as well — return -1 in place of the print — so both branches are terminating and the if itself becomes one. The second is to put a return after the if statement, so the function's last statement is the return and the if no longer has to be terminating at all:
// main.go
package main
import "fmt"
func sign(n int) int {
if n >= 0 {
return 1
} else {
return -1
}
}
func main() {
fmt.Println(sign(3), sign(-3))
}
$ go run main.go
1 -1
The output is 1 -1. This version compiles because both branches return, and the if statement, as the body's last statement, is terminating. The same program with the else branch returning and no statement after it would compile for the same reason; the return-after-the-if form is the one to reach for when the branches do work that should not be duplicated, or when the function has more than two outcomes and the final return is the natural place for the fall-through value.
Switch: returns in the cases, no return after the switch
The same shape appears with a switch, and it is easier to miss there, because a switch that lists cases feels exhaustive. It is not, unless the compiler can see that it is. In the program below, two cases return, but there is no default case, and no statement follows the switch, so an input matching neither case — grade(70) — falls through the switch and off the end of the function:
// main.go
package main
import "fmt"
func grade(score int) string {
switch score {
case 90:
return "A"
case 80:
return "B"
}
}
func main() {
fmt.Println(grade(70))
}
$ go build
# example.com/missingreturn
./main.go:12:1: missing return
The position is the closing brace of grade, line 12. The specification's rule for a switch is a three-part test: there must be no break statements referring to the switch, there must be a default case, and the statement lists in each case, including the default, must end in a terminating statement or a possibly labeled fallthrough statement. This program fails the second part outright — there is no default — so the switch is not terminating even though both cases it does have return. A switch over an integer, or a string, or any type with values the cases do not list, always has an uncovered input until a default case or a following return supplies one.
The repair is either half of that test. Add a default case that returns — default: return "C" — so every case, including the default, ends in a return. Or leave the cases as they are and put a return after the switch, so the uncovered input has a value waiting past it. The fixed program at the end of this guide takes the first form, with a switch that has no tag expression and a default case, and both forms compile. What does not repair it is adding more cases: ten returning cases and no default still leave the eleventh input uncovered, and the compiler still refuses the function.
Return only inside a for loop body
A return inside a loop body is conditional twice over: on the loop running at all, and on the condition inside it being met. The specification's own example of this defect is a function of exactly this shape — a loop over a string that returns when it finds a rune, with no return after the loop — annotated // invalid: missing return statement. The program below is the same shape with a slice:
// main.go
package main
import "fmt"
func find(nums []int, target int) int {
for _, n := range nums {
if n == target {
return n
}
}
}
func main() {
fmt.Println(find([]int{1, 2, 3}, 2))
}
$ go build
# example.com/missingreturn
./main.go:11:1: missing return
The position is the closing brace of find, line 11. Two paths fall through here, not one. An empty slice means the loop body never runs, so no return executes at all. A non-empty slice with no matching element means the loop runs, the inner if never fires, and control still reaches the closing brace. The compiler's rule for a for statement makes this explicit: a for is terminating only when there are no break statements referring to it, the loop condition is absent, and the loop does not use a range clause. A ranged loop fails the third part by construction — the range can always be empty — and a loop with a condition fails the second, because the condition can always be false on entry.
The repair is a return after the loop, holding the value for the not-found path: return -1 after the closing brace of the for, as in the fixed program below. The value it returns is a decision, not a formality — -1 here, an error, a zero value, or a second result value that reports "not found" — and the choice belongs to the function's contract with its caller, which is why the compiler cannot supply it and insists the author write it.
The exception proves the rule, and it is worth seeing once, because it is the only loop that can end a function. A for with no condition, no range clause, and no break cannot exit except through a terminating statement inside it, so the specification counts it as terminating:
// main.go
package main
import "fmt"
func always() int {
for {
return 7
}
}
func main() {
fmt.Println(always())
}
$ go run main.go
7
The output is 7. This program compiles because the loop cannot fall through: the only way out of it is the return. Add a condition, a range clause, or a break referring to the for itself, and the guarantee is gone, and the missing return is back. A break inside a nested switch refers to the switch, not the for, and does not count. In working code this form is rare — a loop that returns on its first iteration is a block with extra steps — and its value here is diagnostic: when a loop with a return still fails, read the loop's header against the three-part test, and the failing part names the repair.
Named result parameters: a bare return is legal, a missing return is not
Named result parameters change what a return has to say, not whether one is required. In the program below, split declares its results by name — (half int, rest int) — assigns them in the body, and then returns with no values listed. The bare return is legal: it returns the current values of the named results, half and rest, in declaration order:
// main.go
package main
import "fmt"
func split(total int) (half int, rest int) {
half = total / 2
rest = total - half
return
}
func main() {
fmt.Println(split(7))
}
$ go run main.go
3 4
The output is 3 4: 7 / 2 is 3, and 7 - 3 is 4. A bare return in a function with named results is a real return, and it satisfies the terminating-statement rule exactly as a return with values does. The names also have a second property worth knowing: they are initialized to their zero values when the function starts, so a bare return taken before any assignment returns zeroes, not an error — a silent wrong answer, not a compile failure, which is why bare returns repay a second look in longer functions.
What a bare return does not do is excuse a path that has no return at all. Assigning a named result is not returning it, and a function that assigns on one path and returns only there still fails on the other path:
// main.go
package main
import "fmt"
func describe(n int) (label string) {
if n > 0 {
label = "positive"
return
}
}
func main() {
fmt.Println(describe(-1))
}
$ go build
# example.com/missingreturn
./main.go:10:1: missing return
The position is the closing brace of describe, line 10. The positive path is complete: it assigns label and returns it bare. The other path — n zero or negative — skips the if entirely, never assigns label, and never returns, so it reaches the end of the body with nothing returned. Naming the result did not change the rule; the specification's requirement is that the function body's statement list end in a terminating statement, and a named result is still a result the function must return. The repair is the same as in the unnamed case: assign the fall-through value and return it — label = "not positive" followed by a bare return after the if — as the fixed program below does. The compiler's refusal is doing the author a service here in a way the zero-value property above makes plain: without it, describe(-1) would silently return "", the zero value of a string, and the caller would have no way to tell "not positive" from "never set".
Return inside a nested block, or inside an if initializer only
A return nested one block deeper than the function's body does not end the function body — it ends the block it sits in, conditionally. The program below returns inside an if that sits inside a bare block, and the block itself, and the function around it, both have paths that skip the return:
// main.go
package main
import "fmt"
func pick(flag bool) int {
{
if flag {
return 1
}
}
}
func main() {
fmt.Println(pick(true))
}
$ go build
# example.com/missingreturn
./main.go:11:1: missing return
The position is the closing brace of pick, line 11. The extra braces change nothing about the analysis and everything about how easy it is to misread: the return 1 is two levels in, behind a condition, and the function body as a whole still has a path — flag false, or the block simply ending — that reaches its own closing brace. A block is terminating only when its statement list ends in a terminating statement, and this block's list ends in an if without an else, which is not terminating. Removing the bare block would not fix the program either; the if alone, without an else that returns, is still not terminating.
The if initializer is the same defect in its most compact form. A variable declared in the initializer — if n := 1; flag — is scoped to the if statement, and a return inside that statement's body covers only the paths that enter it:
// main.go
package main
import "fmt"
func pick(flag bool) int {
if n := 1; flag {
return n
}
}
func main() {
fmt.Println(pick(false))
}
$ go build
# example.com/missingreturn
./main.go:9:1: missing return
The position is the closing brace of pick, line 9. When flag is false, the initializer still runs and then the if body is skipped, so control reaches the end of the function. The repair in both programs is a return after the block or the if statement, at the function's own level: return 0 after the if in the second program, as the fixed program below does. The general test is level, not depth: ask of the function's outermost statement list whether its final statement is terminating, and work inward only from there. A return that is terminating at an inner level counts at the outer level only when every statement that contains it is itself terminating, all the way out.
The contrasts: no result parameters, and no implicit return
Two contrasts close the rule off. First, a function with no result parameters needs no return at all, and a bare return inside it is a way to exit early, not a way to deliver a value:
// main.go
package main
import "fmt"
func greet() {
fmt.Println("hello")
}
func main() {
greet()
}
$ go run main.go
hello
The output is hello. greet declares no results, so its body may end without a terminating statement, and falling off its end is an ordinary return to the caller. A bare return in such a function is legal too — it ends the call early, as in a guard clause that returns before the work — but it returns nothing, and writing return "hello" there would be a different error, a return with a value in a function declared to return none. The missing-return rule fires only when the signature promises results; the signature, not the body's contents, decides.
Second, and worth stating once plainly because every other language habit argues against it: Go has no implicit return. There is no last-expression rule, no fall-through value, no "the function returns whatever its last statement computed". A function that ends with an assignment, a print, or a call still ends with no value returned, and the compiler refuses it exactly as it refuses a function that ends with nothing at all. That refusal is the whole of this guide's error: the compiler will not guess the value, supply a zero value, or treat the last computed value as the result. The author writes the return, on every path, or the program does not build.
The fixed program
One program that applies each fix at once — a return at the end of every function with result parameters, a return after the if and after the for, returns inside the switch cases with a default case, a bare return on both paths of the named-result function, and a return after the if initializer in pick:
// main.go
package main
import "fmt"
func double(n int) int {
return n * 2
}
func sign(n int) int {
if n >= 0 {
return 1
}
return -1
}
func grade(score int) string {
switch {
case score >= 90:
return "A"
case score >= 80:
return "B"
default:
return "C"
}
}
func find(nums []int, target int) int {
for _, n := range nums {
if n == target {
return n
}
}
return -1
}
func describe(n int) (label string) {
if n > 0 {
label = "positive"
return
}
label = "not positive"
return
}
func pick(flag bool) int {
if n := 1; flag {
return n
}
return 0
}
func greet() {
fmt.Println("hello")
}
func main() {
greet()
fmt.Println(double(3), sign(-2), grade(85), find([]int{1, 2, 3}, 2), describe(5), describe(-1), pick(true), pick(false))
}
$ go run main.go
hello
6 -1 B 2 positive not positive 1 0
The output is hello and then 6 -1 B 2 positive not positive 1 0. Each value exercises one fix: double(3) is 6; sign(-2) takes the return after the if and is -1; grade(85) takes the second case and is B; find returns the element it found, 2; describe(5) takes the bare return inside the if and is positive, while describe(-1) takes the assignment and bare return after it and is not positive; pick(true) returns the initializer's 1, and pick(false) takes the return after the if and is 0.
The fixes, ranked
Add a return after the statement that can fall through. This is the fix for the if/else, switch, for, nested-block, and if-initializer shapes: a return at the function's own level, after the statement, holding the value for the path that skips it. It is the most general repair, because it works regardless of which inner shape left the path open, and it puts the fall-through value in one visible place.
Make both branches of the if return, or give the switch a default case that returns. This is the fix when the two outcomes are symmetric and each deserves its own return, or when the switch genuinely covers its inputs once the default is added. Prefer it when the fall-through value would duplicate a branch's value; prefer the return-after form when the branches do work that should not be duplicated.
Assign the named result, then take a bare return — on every path. This is the fix for the named-result shape. A bare return is legal and returns the named results as they stand, but it is a return on one path only. Every path that can reach the end of the body needs one, and the assignment of the fall-through value goes before it, not in place of it.
Return the not-found value after the loop. This is the fix for the return-inside-a-loop shape, and the value is a contract decision: -1, a zero value, an error, or a second result that reports "not found". Choose it from what the caller needs to distinguish, and write it explicitly — the compiler will not supply it.
Do not add more cases, more branches, or more assignments to close the path. More switch cases without a default still leave an uncovered input. More branches that print still leave a non-returning branch. Assigning a named result without returning it still leaves the path open. The test is always the same: does the function's outermost statement list end in a terminating statement, on every path?
Which fix for which cause
| What the code shows | Cause | Fix |
|---|---|---|
| A function with results and no return | The body never returns | Return the value at the end |
One if branch returns, the other prints |
The if is not terminating |
Return in both branches, or after the if |
| Cases return, but no default case | The switch is not terminating |
Add a default that returns, or return after the switch |
| A return only inside a loop body | The loop may not run, or may not match | Return the not-found value after the loop |
| A named result assigned, no return on one path | Assignment is not a return | Assign the fall-through value, then bare return |
| A return inside a nested block | The block is not terminating | Return after the block, at function level |
A return only inside an if initializer |
The if is skipped when false |
Return after the if, at function level |
| A function with no results | No return is required | Nothing to fix; a bare return exits early |
| A value computed but never returned | Go has no implicit return | Write the return explicitly |
Checklist
- Read the position first: it is the closing brace of the function that can fall through, so it names the function, not the path. Find the open path by reading that function's body, path by path.
- Ask of the function's outermost statement list whether its final statement is terminating, using the specification's test: a return, an
ifwith anelsewhere both branches terminate, aforwith no condition, no range, and no break, aswitchwith a default where every case terminates. Any other final statement leaves the path open. - For an
if, check the branch that does not return before assuming the branch that does is the problem. For aswitch, check for a default before counting the cases. For a loop, check the header — a range clause or a condition means the body may never run. - For named results, check every path for a return, not just for an assignment. A path that assigns and falls through is still missing its return, and a bare return before any assignment silently returns zero values.
- When the repair is a return after the statement, choose its value from the caller's contract — what "not found", "negative", or "below every case" should mean — and write it explicitly. The compiler will not choose it for you.
Related guides on this site
- Fix Go "Undefined: ..." Compile Errors — Identifiers, Scope, and Package Qualifiers — the companion lookup failure from the same first hour with the language, where a name has no declaration in scope at the point where it is used, and where the position points at the use rather than at the function's closing brace.
- Fix Go "Declared and Not Used" and "Imported and Not Used" Compile Errors — the companion refusal where a declaration nothing uses is rejected rather than a use nothing returns, and where a declaration added to silence one message can trade it for another.
- Fix Go "No New Variables on Left Side of :=" — Redeclaration, Scope, and Shadowing — the declaration-side scope rules in full, including the
ifinitializer whose variable is scoped to the statement, the same scoping that leaves a return inside anifinitializer covering only the paths that enter it. - Fix Go "Cannot Use ... As ..." Type Errors — Assignability, Conversions, and Untyped Constants — the refusal that comes after a return exists: the name is found, in scope, and the value offered for it does not fit its type.
- Fix Go "Assignment Mismatch" and "Multiple-Value in Single-Value Context" Errors — the counting errors on the same statements, where a return with the wrong number of values fails because the two sides hold different numbers of values.
Sources
- The Go Programming Language Specification — for function declarations (if the function's signature declares result parameters, the function body's statement list must end in a terminating statement; the specification's own example of a loop that returns only inside its body is annotated invalid for a missing return statement), for return statements (the expression list may be empty if the function's result type specifies names for its result parameters, and the return statement then returns the values of those variables, which act as ordinary local variables and are initialized to the zero values for their type upon entry to the function; in a function without a result type, a return statement must not specify any result values), for terminating statements (a return or goto statement, a call to panic, a block whose statement list ends in a terminating statement, an if statement in which the else branch is present and both branches are terminating, a for statement with no break statements referring to it, no loop condition, and no range clause, a switch statement with no break statements referring to it, a default case, and every case ending in a terminating statement, and a labeled statement labeling a terminating statement; all other statements are not terminating), and for blocks and scope (each clause in a switch or select statement acts as an implicit block, as does the body of an if, for, or switch statement; the scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec — the ShortVarDecl for a short variable declaration — and ends at the end of the innermost containing block, so a variable declared by an if initializer belongs to that statement).
Keep Exploring
The full Go hub \u2014 slices and maps, concurrency, and the standard library.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.