-
Notifications
You must be signed in to change notification settings - Fork 0
/
embedded_struct.go
65 lines (55 loc) · 1.51 KB
/
embedded_struct.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import "fmt"
type person struct {
first_name string
last_name string
age int
}
type character struct {
person
game string
age int
}
func main() {
cyb := character{
person: person{ // unqualified type name acts as the field name.
first_name: "", // these cannot be used as field names in composite literal of the struct.
last_name: "Welles",
age: 35,
},
game: "cyberpunk2077",
age: 3,
}
person1 := person{
first_name: "Johnny",
last_name: "Silverhand",
age: 45,
}
person2 := person{
first_name: "",
last_name: "V",
}
person3 := person{
first_name: "Geralt",
last_name: "Of Rivia",
}
cyb1 := character{
person: person1,
game: "cyberpunk2077",
}
cyb2 := character{
person: person2,
game: "cyberpunk2077",
}
wit := character{
person: person3,
game: "witcher",
}
fmt.Printf("Person1 is: %v\nPerson2 is: %v\n", person1, person2)
fmt.Printf("\nMr %v is %v-years old.", person1.last_name, person1.age)
fmt.Printf("\nMr %v is %v-years old.", person2.last_name, person2.age)
fmt.Printf("\nMr %v is %v-years old. The game %v is %v-years old.", cyb.last_name, cyb.person.age, cyb.game, cyb.age) // when the inner type and outer type share the same key, outer one takes priority.
fmt.Printf("\nMr %v of %v is %v-years old.", cyb1.first_name, cyb1.game, cyb1.age)
fmt.Printf("\nMr %v of %v is %v-years old.", cyb2.first_name, cyb2.game, cyb2.age)
fmt.Printf("\nMr %v of %v is %v-years old.", wit.first_name, wit.game, wit.age)
}