Skip to main content

How to Return Early from a Function in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A Go function normally runs to its last line, but return ends it at once. In a function that returns nothing, a plain return with no value is how you leave early, for example right after handling a case that needs nothing more.

Syntax​

Syntax
func <name>(<parameters>) {
if <condition> {
<handle the case>
return
}
<the rest of the function>
}
PartWhat it does
returnEnds the function immediately; nothing after it in the function runs.
No result typeThe function returns nothing, so return must not have a value.

The Go language specification on return statements: "In a function without a result type, a "return" statement must not specify any result values."

The Example​

The example has a greet function that returns nothing. When the name is empty, it prints a message and returns early; otherwise it goes on to greet the person. main calls it with a name and without one.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, where greet prints No name given. and returns when the name is empty, and otherwise prints Hello and the name; main calls greet with Ada and with an empty string.

main.go
package main

import "fmt"

func greet(name string) {
if name == "" {
fmt.Println("No name given.")
return
}
fmt.Println("Hello, " + name + "!")
}

func main() {
greet("Ada")
greet("")
}

Step 2: Run It​

Terminal
go run main.go
Output
Hello, Ada!
No name given.

A zsh terminal where go run main.go prints Hello, Ada! and No name given.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • greet("Ada") skipped the if block and printed the greeting.
  • greet("") entered the if block, printed its message, and return ended the function before the greeting line.

Common Mistakes​

MistakeWhat happensDo this instead
Returning a value from a function without a result typeIt doesn't compile: too many return values, with have (string) and want ()Remove the value, or give the function a result type
Forgetting the return in the guardThe function goes on and runs the code meant for the other caseEnd the guard block with return
Nesting the main work inside elseThe code drifts right and gets harder to readReturn early and keep the main path unindented

Troubleshooting​

SymptomLikely causeFix
too many return valuesreturn has a value in a function with no result typeDrop the value
Both messages printed for one callThe guard block has no returnAdd return at the end of the guard

FAQ​

Can I return early from main?​

Yes. return in main ends the program normally, with exit code 0. To end with another code, use os.Exit.

What about functions that return a value?​

There, every return needs values for the results, as in return "", err in a function that returns a string and an error.

Conclusion​

return on its own leaves a Go function that returns nothing, right away. Use it at the end of a guard clause so the special case is handled and done, and the rest of the function stays simple.

Additional Resources​