Functions, multiple returns and errors in Go

Learn Go · Beginner · 11 min · After Slices and maps in Go

How does a Go function tell you it failed?

  1. 1Guess

    package main
    
    import (
    	"fmt"
    	"strconv"
    )
    
    func main() {
    	n, err := strconv.Atoi("12a")
    	fmt.Println(n, err)
    }
    
    What does this program print?
  2. 2Read

    Functions and results

    A function lists its parameters with types and its results after them: func add(a, b int) int. A function can return more than one value: func divide(a, b float64) (float64, error). The caller takes them with x, err := divide(8, 2). Use _ for a value you really do not need.

  3. 3Code

    Return a value and an error

    1 / 2
    package main
    
    import (
    	"errors"
    	"fmt"
    )
    
    func divide(a, b float64) (float64, error) {
    	if b == 0 {
    		return 0, errors.New("divide by zero")
    	}
    	return a / b, nil
    }
    
    func main() {
    	for _, d := range []float64{4, 0} {
    		result, err := divide(8, d)
    		if err != nil {
    			fmt.Println("error:", err)
    			continue
    		}
    		fmt.Println("result:", result)
    	}
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Errors are values

    The error type is an ordinary interface: any value with an Error() string method is an error. Check it right after the call with if err != nil. To add context, wrap it: fmt.Errorf("parse age: %w", err). The %w verb keeps the original error inside, so errors.Is(err, target) can still find it.

  5. 5Code

    Wrap an error and test it with errors.Is

    1 / 2
    package main
    
    import (
    	"errors"
    	"fmt"
    	"strconv"
    )
    
    var ErrEmpty = errors.New("empty input")
    
    func parseAge(s string) (int, error) {
    	if s == "" {
    		return 0, ErrEmpty
    	}
    	n, err := strconv.Atoi(s)
    	if err != nil {
    		return 0, fmt.Errorf("parse age: %w", err)
    	}
    	return n, nil
    }
    
    func main() {
    	_, err := parseAge("")
    	fmt.Println(err, errors.Is(err, ErrEmpty))
    	_, err = parseAge("x")
    	fmt.Println(err, errors.Is(err, ErrEmpty))
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    package main
    
    import (
    	"fmt"
    	"strconv"
    )
    
    func main() {
    	input := "4o"
    	n, _ := strconv.Atoi(input)
    	fmt.Println("Doubled:", n*2)
    }
    
    This program compiles but prints Doubled: 0 for the input 4o. What is the bug?
  7. 7Say it back

  8. 8Mission · 10 min

    Write a function that parses an age from text and returns an int and an error. Reject empty text with a sentinel error and wrap other failures. Call it with three inputs and print what happens.

    go run .

Common questions

How do I return an error in Go?
Add error as the last result type, then return a value and an error. On success return the value and nil; on failure return a zero value and an error made with errors.New or fmt.Errorf. The caller checks the error right after the call with if err != nil, which keeps failures visible in the code instead of hidden.
What does %w do in fmt.Errorf?
The %w verb wraps an error: the new error prints your message with the original text, and it keeps the original inside. Later, errors.Is and errors.As can look through the wrapping to find it. Use it to add context, such as which step failed, without losing the original error value that callers may want to test.
What is the difference between errors.New and fmt.Errorf?
Both create an error value. The errors.New function takes a fixed message, which suits a sentinel error such as ErrEmpty that callers compare against. The fmt.Errorf function builds a message from a format string and values, and with %w it can wrap another error. Use New for fixed errors and Errorf when you add details or context.

Sources

Measured with go1.27.4 darwin/arm64 on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer