go_playground/5-go-by-example/21-embedding.go

49 lines
1000 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// embedding of structs and interfaces to express a more seamless composition of types
package main
import "fmt"
type base struct {
num int
}
func (b base) describe() string {
return fmt.Sprintf("base with num=%v", b.num)
}
// container embeds a base
// embedding looks like a field without a name
type container struct {
base
str string
}
func main() {
// when creating structs with literals, initialize the embedding explicitly
co := container{
base: base{
num: 1,
},
str: "some name",
}
// access the bases fields directly
fmt.Printf("co={num: %v, str: %v}\n", co.num, co.str)
// full path using the embedded type name
fmt.Println("also num:", co.base.num)
// container embeds base
// methods of base also become methods of a container
fmt.Println("describe:", co.describe())
type describer interface {
describe() string
}
// container implements describer interface because it embeds base
var d describer = co
fmt.Println("describer:", d.describe())
}