July 2019
Intermediate to advanced
502 pages
14h
English
Go is a modern language and recognizes the importance of testing. Go encourages that for each foo.go file you have, to have foo_test.go. It also provides the testing package, and the Go tool has a test command. Let's look at a simple example. Here is a foo.go file that contains the safeDivide() function. This function divides integers and returns a result and an error.
If the denominator is non-zero, it returns no error, but if the denominator is zero, it returns a division by zero error:
package main import "errors" func safeDivide(a int, b int) (int, error) { if b == 0 { return 0, errors.New("division by zero") } return a / b, nil }
Note that Go division uses integer division when both operands are integers. This ...