package main
type A struct {
g int
}
func (A) m() int {
return 1
}
type B int
func (B) g() {}
func (B) f() {}
type C struct{
A
B
}
func (C) m() int {
return 9
}
func main() {
var c interface{} = C{}
_, bf := c.(interface{f()})
_, bg := c.(interface{g()})
i := c.(interface{m() int})
println(bf, bg, i.m())
}
Choices:
Answer: true false 9
Run it on Go play.
Key points:
C.A.g
and method C.B.g
collide, so they are both not promoted.
C.B.f
gets promoted as C.f
.
C.m
overrides C.A.m
.
Please read type embedding for more detailed explanations.
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.