September 2017
Intermediate to advanced
466 pages
9h 33m
English
Go functions can return error variables, which means that an error condition can be handled inside a function, outside of a function, or both inside and outside the function; the latter situation does not happen very often. So, this subsection will develop a function that returns error messages. The relevant Go code can be found in funErr.go and will be presented in three parts.
The first part contains the following Go code:
package main import ( "errors" "fmt" "log" ) func division(x, y int) (int, error, error) { if y == 0 { return 0, nil, errors.New("Cannot divide by zero!") } if x%y != 0 { remainder := errors.New("There is a remainder!") return x / y, remainder, nil } else { return x / y, nil, nil } ...Read now
Unlock full access