Add example of struct embedding in Go
Also mention implementing interfaces with embedding Fixes #381 Fixes #398
This commit is contained in:
59
examples/embedding/embedding.go
Normal file
59
examples/embedding/embedding.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Go support _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)
|
||||
}
|
||||
|
||||
// A `container` _embeds_ a `base`. An embedding looks
|
||||
// like a field without a name.
|
||||
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 `co`,
|
||||
// e.g. `co.num`.
|
||||
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 `container` embeds `base`, the methods of
|
||||
// `base` also become methods of a `container`. Here
|
||||
// we invoke a method that was embedded from `base`
|
||||
// directly on `co`.
|
||||
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 `container` now implements the
|
||||
// `describer` interface because it embeds `base`.
|
||||
var d describer = co
|
||||
fmt.Println("describer:", d.describe())
|
||||
}
|
||||
2
examples/embedding/embedding.hash
Normal file
2
examples/embedding/embedding.hash
Normal file
@@ -0,0 +1,2 @@
|
||||
8a15291c6e82b9c6873002e2aa5cef65907a3405
|
||||
Xtg07j06zBv
|
||||
5
examples/embedding/embedding.sh
Normal file
5
examples/embedding/embedding.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
$ go run embedding.go
|
||||
co={num: 1, str: some name}
|
||||
also num: 1
|
||||
describe: base with num=1
|
||||
describer: base with num=1
|
||||
Reference in New Issue
Block a user