![Learning Functional Programming in Go](https://wfqqreader-1252317822.image.myqcloud.com/cover/487/36700487/b_36700487.jpg)
It's time to run our program
Now, it's time to provide the duck with its starting resources and a list of ponds to swim in and see whether our duck survives to live another day.
Let's assume our duck has five bugs in its belly, which is worth five strokes (we made our ponds and bugs very small to simplify our model):
func main() {
var duck Duck
capabilities := Capabilities{
StrokeBehavior: Foot{},
EatBehavior: Bill{},
strokes: 5,
}
Our duck's first set of ponds will consist of two ponds. Each supplies only one bug. The first pond requires three strokes to reach the other side. The second pond requires two strokes:
ponds := []Pond{
{BugSupply: 1, StrokesRequired: 3},
{BugSupply: 1, StrokesRequired: 2},
}
duck.SwimAndEat(&capabilities, &capabilities.strokes, ponds)
displayDuckStats(&capabilities, ponds)
The call to the duck's SwimAndEat method uses the address of its capabilities because we want to share the duck's Capabilities object as our duck moves from one set of ponds to another.
At the end of each day, after the duck has crossed each pond and eaten the bugs it finds, we display the duck's statistics:
func displayDuckStats(c *Capabilities, ponds []Pond) {
fmt.Printf("%s\n", DASHES)
fmt.Printf("Ponds Processed:")
for _, pond := range ponds {
fmt.Printf("\n\t%+v", pond)
}
fmt.Printf("\nStrokes remaining: %+v\n", c.strokes)
fmt.Printf("%s\n\n", DASHES)
}
Here's the output of this:
- Foot, paddle!
- Foot, paddle!
- Foot, paddle!
- Bill, eat a bug!
- Foot, paddle!
- Foot, paddle!
- Bill, eat a bug!
----------------------
Ponds Processed:
{BugSupply:1 StrokesRequired:3}
{BugSupply:1 StrokesRequired:2}
Strokes remaining: 2
----------------------
At the end of the first day, the duck crossed two ponds and has two strokes in reserve to start a new day.
The next day, our duck has only one pond to swim. Our duck has two bugs in its belly. There're two bugs in this pond. Let's see whether our duck makes it to the other side:
ponds = []Pond{
{BugSupply: 2, StrokesRequired: 3},
}
duck.SwimAndEat(&capabilities, &capabilities.strokes, ponds)
displayDuckStats(&capabilities, ponds)
Here's the output of this:
- Foot, paddle!
- Foot, paddle!
- Foot, paddle!
2017/05/12 19:11:51 Our duck died!
exit status 1
Unfortunately, our duck did not have enough strokes to cross the pond. Bummer!
![](https://epubservercos.yuewen.com/6176FA/19470400908922906/epubprivate/OEBPS/Images/Chapter_16.jpg?sign=1739367286-7Nu6trKVUD6RfAEThT5x5yrCIM4EkTIJ-0-69139a0e9a9393858c7582f0e146e5d0)
The moral of our story is as follows:
- Model applications in meaningful (like real world) ways
- Start by creating a set of behaviors as single responsibility interface types
- Compose simple interface types into larger, coherent sets of behaviors
- Ensure each function accepts only the types of behaviors it requires
- Don't be a duck