- Learning Functional Programming in Go
- Lex Sheehan
- 450字
- 2025-02-27 05:14:35
Pass by value or reference?
Here's the rule of thumb--if you want to share a state, then pass by reference, that is, use a pointer type; otherwise, pass by value. Since we need to update our duck's strokeSupply type in this Stroke method, we pass it as an int pointer (*int). So, pass a pointer parameter only when absolutely necessary. We should begin to code defensively, assuming that someone may try to run our code concurrently. When we pass our parameters by value, it's safe for concurrent use. When we pass by reference, we may need to add sync.mutex or some channels to coordinate concurrency.
Our duck builds its energy back by eating more bugs that it gets from the pond:
func (Duck) Eat(e EatBehavior, strokeSupply *int, p Pond) {
for i := 0; i < p.BugSupply; i++ {
e.EatBug(strokeSupply)
}
}
Since we are designing our software application to model the real world, things such as duck feet and duck bills are natural candidates for struct names to represent real-life objects. Feet are used to paddle and duck bills are used to eat bugs. Each paddle, that is, stroke, reduces our duck's supply of possible strokes. Each bug is worth one stroke.
We tell our duck's foot to paddle. As long as the duck has energy, that is, it's strokeSupply type is greater than zero, the duck will obey. However, if strokeSupply is zero, then our duck will be stranded in the middle of the pond before it gets to its next supply of bugs to eat:
type Foot struct{}
func (Foot) PaddleFoot(strokeSupply *int) {
fmt.Println("- Foot, paddle!")
*strokeSupply--
}
Notice that we are passing a pointer to our supply of strokes. This means that our application is maintaining a state. We know that pure functional programming does not permit variable mutations. That's okay because this chapter is about good software design using Go. Pure functional programming in Go is covered in Chapter 1, Pure Functional Programming in Go:
type Bill struct{}
func (Bill) EatBug(strokeSupply *int) {
*strokeSupply++
fmt.Println("- Bill, eat a bug!")
}
For every pond that our duck encounters, it must swim and eat bugs to survive.
Since our duck's SwimAndEat method requires both StrokeBehavior and EatBehavior, we pass the SurvivalEatBehaviors interface set as its first parameter:
func (d Duck) SwimAndEat(se SurvivalBehaviors, strokeSupply *int, ponds []Pond) {
for i := range ponds {
pond := &ponds[i]
err := d.Stroke(se, strokeSupply, pond)
if err != nil {
log.Fatal(err) // the duck died!
}
d.Eat(se, strokeSupply, pond)
}
}
Recall that the duck's Stroke method takes StrokeBehavior, not StrokeEatBehavior! How is this possible? This is part of the magic of type embedding.