-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbedded.go
42 lines (34 loc) · 938 Bytes
/
Embedded.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"github.com/kr/pretty"
)
func main() {
type Animal struct {
Name string
Origin string
}
//A Bird has Animal characteristics
type Bird struct {
Animal //anonymous embedded struct - this simplifies dereferencing field values later
Speed int
CanFly bool
}
//literal declaration to assign values
b := Bird{
Animal: Animal{Name: "emu", Origin: "Australia"}, //note how the embedded struct is exposed
Speed: 48,
CanFly: false,
}
fmt.Printf("title: %v, words:%v, isbn:%v\n", b.text.title, b.text.wordCount, b.isbn)
//title: Romeo and Juliet, words:11657, isbn:1097834
fmt.Printf("title: %v, words:%v, isbn:%v\n", b.title, b.wordCount, b.isbn)
//title: Romeo and Juliet, words:11657, isbn:1097834
fmt.Printf("%# v\n", pretty.Formatter(b))
/* pretty.Formatter output
main.book{
text: main.text{title:"Romeo and Juliet", wordCount:11657},
isbn: "1097834",
}
*/
}