Junior — Senior
Transforming one interface into another
livecode
Task condition
In the example, there is a type Bar that implements methods X(), Y(), and Z(). Based on these, two interfaces are declared: XY, requiring methods X() and Y(), and YZ, requiring methods Y() and Z(). Question: can a variable of type XY be cast to type YZ?
type Bar struct{}
func (b *Bar) X() {}
func (b *Bar) Y() {}
func (b *Bar) Z() {}
type XY interface {
X()
Y()
}
type YZ interface {
Y()
Z()
}
func main() {
var b XY = &Bar{}
z := b.(YZ) // Will this cast work?
z.X() // And this call?
_ = z
}
Describe how interface casting works in Go, what conditions must be met, and what will happen if you try to call a method that is not present in the target interface.