Skip to content

Commit cf5a398

Browse files
committed
fixing code formating and removing redundant comments
1 parent 5315ab3 commit cf5a398

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

en/02.4.md

+17-25
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type person struct {
88
name string
99
age int
1010
}
11-
```
11+
```
1212
Look how easy it is to define a `struct`!
1313

1414
There are two fields.
@@ -34,7 +34,7 @@ There are three more ways to define a struct.
3434
- Assign initial values by order
3535
```Go
3636
P := person{"Tom", 25}
37-
```
37+
```
3838
- Use the format `field:value` to initialize the struct without order
3939
```Go
4040
P := person{age:24, name:"Bob"}
@@ -44,6 +44,7 @@ P := person{age:24, name:"Bob"}
4444
P := struct{name string; age int}{"Amy",18}
4545
```
4646
Let's see a complete example.
47+
4748
```Go
4849
package main
4950

@@ -55,8 +56,8 @@ type person struct {
5556
age int
5657
}
5758

58-
// compare the age of two people, then return the older person and differences of age
5959
// struct is passed by value
60+
// compare the age of two people, then return the older person and differences of age
6061
func Older(p1, p2 person) (person, int) {
6162
if p1.age > p2.age {
6263
return p1, p1.age - p2.age
@@ -67,27 +68,19 @@ func Older(p1, p2 person) (person, int) {
6768
func main() {
6869
var tom person
6970

70-
// initialization
7171
tom.name, tom.age = "Tom", 18
72-
73-
// initialize two values by format "field:value"
7472
bob := person{age: 25, name: "Bob"}
75-
76-
// initialize two values with order
7773
paul := person{"Paul", 43}
7874

7975
tb_Older, tb_diff := Older(tom, bob)
8076
tp_Older, tp_diff := Older(tom, paul)
8177
bp_Older, bp_diff := Older(bob, paul)
8278

8379
fmt.Printf("Of %s and %s, %s is older by %d years\n", tom.name, bob.name, tb_Older.name, tb_diff)
84-
8580
fmt.Printf("Of %s and %s, %s is older by %d years\n", tom.name, paul.name, tp_Older.name, tp_diff)
86-
8781
fmt.Printf("Of %s and %s, %s is older by %d years\n", bob.name, paul.name, bp_Older.name, bp_diff)
8882
}
89-
90-
```
83+
```
9184
### embedded fields in struct
9285

9386
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 {
112105
}
113106

114107
func main() {
115-
// initialize a student
108+
// instantiate and initialize a student
116109
mark := Student{Human{"Mark", 25, 120}, "Computer Science"}
117110

118111
// access fields
119112
fmt.Println("His name is ", mark.name)
120113
fmt.Println("His age is ", mark.age)
121114
fmt.Println("His weight is ", mark.weight)
122115
fmt.Println("His specialty is ", mark.specialty)
123-
// modify notes
116+
117+
// modify mark's specialty
124118
mark.specialty = "AI"
125119
fmt.Println("Mark changed his specialty")
126120
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")
129123
mark.age = 46
130-
fmt.Println("His age is", mark.age)
131-
// modify weight
132-
fmt.Println("Mark is not an athlet anymore")
133124
mark.weight += 60
125+
fmt.Println("His age is", mark.age)
134126
fmt.Println("His weight is", mark.weight)
135127
}
136128

137-
```
129+
```
138130
![](images/2.4.student_struct.png?raw=true)
139131

140132
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
143135
```Go
144136
mark.Human = Human{"Marcus", 55, 220}
145137
mark.Human.age -= 1
146-
```
138+
```
147139
All the types in Go can be used as embedded fields.
148140
```Go
149141
package main
@@ -184,7 +176,7 @@ func main() {
184176
fmt.Println("Her preferred number is ", jane.int)
185177
}
186178

187-
```
179+
```
188180
In the above example, we can see that all types can be embedded fields and we can use functions to operate on them.
189181

190182
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 {
202194
}
203195

204196
type Employee struct {
205-
Human // embedded field Human
197+
Human
206198
specialty string
207199
phone string // phone in employee
208200
}
209201

210202
func main() {
211203
Bob := Employee{Human{"Bob", 34, "777-444-XXXX"}, "Designer", "333-222"}
204+
212205
fmt.Println("Bob's work phone is:", Bob.phone)
213-
// access phone field in Human
214206
fmt.Println("Bob's personal phone is:", Bob.Human.phone)
215207
}
216208

217-
```
209+
```
218210
## Links
219211

220212
- [Directory](preface.md)

0 commit comments

Comments
 (0)