Tsallaka zuwa abun ciki
Justice Tech School

Errors

Functions return an error as the last value. Check it right away.

Example
package main

import (
	"fmt"
	"strconv"
)

func parse(s string) (int, error) { return strconv.Atoi(s) }

func main() {
	v, err := parse("42")
	fmt.Println(v, err)
	_, err = parse("x")
	fmt.Println(err)
}

Result

42 <nil>
strconv.Atoi: parsing "x": invalid syntax

Exercise

Check the error.

if err  nil

Quiz Test

What does a nil error mean?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.