November 2017
Intermediate to advanced
670 pages
17h 35m
English
First, let's create a Car struct that we can use to define the Cars collection as a slice of Car. Later, we'll create a Contains() method to try out on our collection:
package maintype Car struct { Make string Model string}type Cars []*Car
Here's our Contains() implementation. Contains() is a method for Cars. It takes a modelName string, for example, Highlander, and returns true if it was found in the slice of Cars:
func (cars Cars) Contains(modelName string) bool { for _, a := range cars { if a.Model == modelName { return true } } return false}
This seems simple enough to implement, but what happens when we are given a list of boats or boxes to iterate over? That's right, we'll have to reimplement the ...