Go provides a special data type, named error
, for representing error conditions and error messages—in practice, this means that Go treats errors as values. To program successfully in Go, you should be aware of the error conditions that might occur with the functions and methods you are using and handle them accordingly.
As you already know from the previous chapter, Go follows a particular convention concerning error values: if the value of an error
variable is nil
, then there is no error. As an example, let us consider strconv.Atoi()
, which is used for converting a string
value into an int
value (Atoi
stands for ASCII to Int). As specified by its signature, strconv.Atoi()
returns (int, error)
. Having an error value of nil
means that the conversion was successful and that you can use the int
value if you want. Having an error value that is not nil
means that the conversion was unsuccessful and that the string input is not a valid int
value.
...