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
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.