Working with unit tests
We will work on an example function that sorts time.Time
values in ascending or descending order, which is given here:
package sort import ( "sort" "time" ) // Sort times in ascending or descending order func SortTimes(input []time.Time, asc bool) []time.Time { output := make([]time.Time, len(input)) copy(output, input) if asc { sort.Slice(output, func(i, j int) bool { return output[i].Before(output[j]) }) return output } sort.Slice(output, func(i, j int) bool { return output[j].Before(output[i]) }) return output }
We will use the built-in testing tools provided by the Go build system and the standard library. For this, let’s suppose we stored the preceding function in a file called sort.go
...