@@ -8,7 +8,7 @@ type person struct {
8
8
name string
9
9
age int
10
10
}
11
- ```
11
+ ```
12
12
Look how easy it is to define a ` struct ` !
13
13
14
14
There are two fields.
@@ -34,7 +34,7 @@ There are three more ways to define a struct.
34
34
- Assign initial values by order
35
35
``` Go
36
36
P := person{" Tom" , 25 }
37
- ```
37
+ ```
38
38
- Use the format ` field:value ` to initialize the struct without order
39
39
``` Go
40
40
P := person{age:24 , name:" Bob" }
@@ -44,6 +44,7 @@ P := person{age:24, name:"Bob"}
44
44
P := struct {name string ; age int }{" Amy" ,18 }
45
45
```
46
46
Let's see a complete example.
47
+
47
48
``` Go
48
49
package main
49
50
@@ -55,8 +56,8 @@ type person struct {
55
56
age int
56
57
}
57
58
58
- // compare the age of two people, then return the older person and differences of age
59
59
// struct is passed by value
60
+ // compare the age of two people, then return the older person and differences of age
60
61
func Older (p1 , p2 person ) (person , int ) {
61
62
if p1.age > p2.age {
62
63
return p1, p1.age - p2.age
@@ -67,27 +68,19 @@ func Older(p1, p2 person) (person, int) {
67
68
func main () {
68
69
var tom person
69
70
70
- // initialization
71
71
tom.name , tom.age = " Tom" , 18
72
-
73
- // initialize two values by format "field:value"
74
72
bob := person{age: 25 , name: " Bob" }
75
-
76
- // initialize two values with order
77
73
paul := person{" Paul" , 43 }
78
74
79
75
tb_Older , tb_diff := Older (tom, bob)
80
76
tp_Older , tp_diff := Older (tom, paul)
81
77
bp_Older , bp_diff := Older (bob, paul)
82
78
83
79
fmt.Printf (" Of %s and %s , %s is older by %d years\n " , tom.name , bob.name , tb_Older.name , tb_diff)
84
-
85
80
fmt.Printf (" Of %s and %s , %s is older by %d years\n " , tom.name , paul.name , tp_Older.name , tp_diff)
86
-
87
81
fmt.Printf (" Of %s and %s , %s is older by %d years\n " , bob.name , paul.name , bp_Older.name , bp_diff)
88
82
}
89
-
90
- ```
83
+ ```
91
84
### embedded fields in struct
92
85
93
86
I've just introduced to you how to define a struct with field names and type. In fact, Go supports fields without names, but with types. We call these embedded fields.
@@ -112,29 +105,28 @@ type Student struct {
112
105
}
113
106
114
107
func main () {
115
- // initialize a student
108
+ // instantiate and initialize a student
116
109
mark := Student{Human{" Mark" , 25 , 120 }, " Computer Science" }
117
110
118
111
// access fields
119
112
fmt.Println (" His name is " , mark.name )
120
113
fmt.Println (" His age is " , mark.age )
121
114
fmt.Println (" His weight is " , mark.weight )
122
115
fmt.Println (" His specialty is " , mark.specialty )
123
- // modify notes
116
+
117
+ // modify mark's specialty
124
118
mark.specialty = " AI"
125
119
fmt.Println (" Mark changed his specialty" )
126
120
fmt.Println (" His specialty is " , mark.specialty )
127
- // modify age
128
- fmt.Println (" Mark become old" )
121
+
122
+ fmt.Println (" Mark become old. He is not an athlete anymore " )
129
123
mark.age = 46
130
- fmt.Println (" His age is" , mark.age )
131
- // modify weight
132
- fmt.Println (" Mark is not an athlet anymore" )
133
124
mark.weight += 60
125
+ fmt.Println (" His age is" , mark.age )
134
126
fmt.Println (" His weight is" , mark.weight )
135
127
}
136
128
137
- ```
129
+ ```
138
130
![ ] ( images/2.4.student_struct.png?raw=true )
139
131
140
132
Figure 2.7 Embedding in Student and Human
@@ -143,7 +135,7 @@ We see that we can access the `age` and `name` fields in Student just like we ca
143
135
``` Go
144
136
mark.Human = Human {" Marcus" , 55 , 220 }
145
137
mark.Human .age -= 1
146
- ```
138
+ ```
147
139
All the types in Go can be used as embedded fields.
148
140
``` Go
149
141
package main
@@ -184,7 +176,7 @@ func main() {
184
176
fmt.Println (" Her preferred number is " , jane.int )
185
177
}
186
178
187
- ```
179
+ ```
188
180
In the above example, we can see that all types can be embedded fields and we can use functions to operate on them.
189
181
190
182
There is one more problem however. If Human has a field called ` phone ` and Student has a field with same name, what should we do?
@@ -202,19 +194,19 @@ type Human struct {
202
194
}
203
195
204
196
type Employee struct {
205
- Human // embedded field Human
197
+ Human
206
198
specialty string
207
199
phone string // phone in employee
208
200
}
209
201
210
202
func main () {
211
203
Bob := Employee{Human{" Bob" , 34 , " 777-444-XXXX" }, " Designer" , " 333-222" }
204
+
212
205
fmt.Println (" Bob's work phone is:" , Bob.phone )
213
- // access phone field in Human
214
206
fmt.Println (" Bob's personal phone is:" , Bob.Human .phone )
215
207
}
216
208
217
- ```
209
+ ```
218
210
## Links
219
211
220
212
- [ Directory] ( preface.md )
0 commit comments