November 2017
Intermediate to advanced
670 pages
17h 35m
English
Go allows us to declare a type inside another type. In our SurvivalBehaviors interface, we have declared two fields of type interface. Through inner type promotion, the Go compiler performs interface conversions and the inner interface becomes part of the outer interface:
type SurvivalBehaviors interface { StrokeBehavior EatBehavior}
The d.Stroke function takes a SurvivalBehaviors type as though it received StrokeBehavior, and the d.Eat function takes a SurvivalBehaviors type as if it received EatBehavior.
This means that the outer type, SurvivalBehaviors, now implements the interface of both StrokeBehavior and EatBehavior.