The car functor
Let's use a functor to upgrade (and downgrade) some cars! We'll start by opening our car.go file in our functor package.
The functor package
Let's have a look at src/functor/car.go:
package functor
import (
"fmt"
"strings"
)
type (
Car struct {
Make string `json:"make"`
Model string `json:"model"`
}
)It's good practice to define our types at the top. Putting them in a type block helps to keep our code clean and tidy. Another good practice is to add JSON annotations to each field of a struct to enable easy (un)marshalling of JSON into our Car struct.
Note
If you want to omit empty fields from a struct, you can add the omitempty clause to the end of your field annotation. For example, if the Make was optional or sometimes not included and we didn't want the json created from a Car struct to include empty Make fields, our struct definition would look like this:Car struct { Make string `json:"make"` Model string `json:"model,omitempty"`}
Next comes our interface...