Skip to content

Latest commit

 

History

History
71 lines (50 loc) · 1.61 KB

4.2.md

File metadata and controls

71 lines (50 loc) · 1.61 KB

Errors

Concepts

Many programming languages allow throwing and catching errors, which allows developers to skip error handling in much of the function stack. Go strongly discurages this and instead guides programmers to use the errors package. This allows programmers to know exactly what inputs and outputs they have to deal with at the expense of verbosity.

package main

import (
    "fmt"
    "errors"
)

func main() {
    val, err := doubleOrError(10)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Println("Success")

}

func doubleOrError(x int) (int, error) {
    if x % 2 == 0 {
        return 0, errors.New("x must be odd")
    }
    return x, nil
}

The important bits from this are:

val, err := doubleOrError(10)

  • val and err capture the return values of the function.

if err != nil {

  • Handle the error appropriately if it exists.

fmt.Println(err.Error())

  • err.Error() returns the string representation of the error.

return 0, errors.New("x must be odd")

  • Even though we are returning an error, we must return all types declared in the function signature.
  • errors.New creates a new error.

Exercises

Implement a square root function:

  • What should the function signature look like?
  • What values are not excepted?

Tips

The type error is actually an interface:

type error interface {
    Error() string
}

This means you can create more meaningful error types.

Further Reading

The source code of errors is simple, go check it out!


prev -- up -- next