Chapter 44: Go Function Returns

Functions return values (especially multiple return values — the thing that makes Go feel so clean and safe compared to many other languages).

In Go, returning values is not just a detail — it’s a core part of the language’s philosophy:

  • be explicit
  • handle errors properly
  • avoid exceptions / panics for normal control flow
  • make code readable and maintainable

Let me explain everything about Go function returns like your personal teacher sitting next to you — slowly, clearly, with many copy-paste examples, real-world patterns, style notes, and the “why” behind each decision.

1. Basic Return – Single Value

The simplest form:

Go
Go

Key points:

  • Return type is written after the parameter list
  • You must return exactly one value of the declared type (or panic)
  • return statement is mandatory unless the function has no return type

2. No Return Value (Void-like functions)

Go

→ No return needed at the end (but you can write return early if you want to exit)

3. Multiple Return Values – The Go Superpower

This is the feature that makes Go feel different and better for real-world code.

Go
Go

Why multiple returns are so common in Go (especially with error):

  • Go has no exceptions / no try-catch for normal errors
  • Errors are values — you handle them explicitly
  • Most functions that can fail return (something, error)
  • Caller must check err != nil (compiler doesn’t force it — but community tools like golangci-lint can warn you)

4. Named Return Values – Very Idiomatic & Powerful

When you give names to return values, they are automatically declared as variables inside the function (zero-initialized) and you can use naked return (return without listing values).

Go
Go

Even more realistic example (very common pattern):

Go

Benefits of named returns:

  • Signature becomes self-documenting ((name string, age int, err error))
  • Naked return reduces repetition
  • Very useful when you have early returns on error

Community advice 2025–2026:

  • Use named returns when there are 2 or more return values
  • Especially when one of them is error
  • Avoid named returns for single-return functions (unnecessary)

5. Ignoring Return Values (Blank Identifier _)

Go

Rule: Go forbids unused variables — so if you don’t need a return value, use _

6. Quick Practice – Try Writing These

  1. Function minMax(numbers …int) (min, max int) that returns smallest & largest value
  2. Function getAreaAndPerimeter(length, width float64) (area, perimeter float64)
  3. Function parseInt(s string) (n int, err error) that tries strconv.Atoi
  4. Function login(username, password string) (success bool, message string)

Which one did you enjoy writing most?

Any part still confusing?

  • Why Go prefers (value, error) instead of exceptions?
  • When to use named returns vs plain returns?
  • Multiple returns vs single return + struct?
  • Or ready for pointers as return values / defer next?

Keep writing small functions with different return patterns — this is the moment when you go from “learning syntax” to “building real reusable logic”.

You’re progressing really fast — keep asking! 💪🇮🇳🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *