|
Go supports embedding of structs and interfaces
to express a more seamless composition of types.
This is not to be confused with |
|
![]()
package main |
|
import "fmt" |
|
type base struct { num int } |
|
func (b base) describe() string { return fmt.Sprintf("base with num=%v", b.num) } |
|
|
A |
type container struct { base str string } |
func main() { |
|
|
When creating structs with literals, we have to initialize the embedding explicitly; here the embedded type serves as the field name. |
co := container{
base: base{
num: 1,
},
str: "some name",
}
|
|
We can access the base’s fields directly on |
fmt.Printf("co={num: %v, str: %v}\n", co.num, co.str)
|
|
Alternatively, we can spell out the full path using the embedded type name. |
fmt.Println("also num:", co.base.num)
|
|
Since |
fmt.Println("describe:", co.describe())
|
type describer interface { describe() string } |
|
|
Embedding structs with methods may be used to bestow
interface implementations onto other structs. Here
we see that a |
var d describer = co
fmt.Println("describer:", d.describe())
}
|
$ go run struct-embedding.go co={num: 1, str: some name} also num: 1 describe: base with num=1 describer: base with num=1 |
Next example: Generics.