Learn / Go / Errors, the Go way

Go, lesson 4 of 4

Errors, the Go way

Go does not hide failures. A function that can fail returns two things: the value, and an error. You check the error before using the value.

n, err := strconv.Atoi("42")
if err != nil {
    fmt.Println("not a number")
    return
}
fmt.Println(n)

strconv.Atoi turns text into a number. Given "42x", it fails, so err is not nil.

Your turn

Try to turn "42x" into a number. Since it is not valid, print exactly not a number.

Press Run to see what your code prints.