From cf5a39886db5fbb32b763a911c6bf6223d61be87 Mon Sep 17 00:00:00 2001 From: ldynia Date: Sun, 16 Jul 2017 18:29:38 +0200 Subject: [PATCH] fixing code formating and removing redundant comments --- en/02.4.md | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/en/02.4.md b/en/02.4.md index e80360da..edd8f818 100644 --- a/en/02.4.md +++ b/en/02.4.md @@ -8,7 +8,7 @@ type person struct { name string age int } -``` +``` Look how easy it is to define a `struct`! There are two fields. @@ -34,7 +34,7 @@ There are three more ways to define a struct. - Assign initial values by order ```Go P := person{"Tom", 25} -``` +``` - Use the format `field:value` to initialize the struct without order ```Go P := person{age:24, name:"Bob"} @@ -44,6 +44,7 @@ P := person{age:24, name:"Bob"} P := struct{name string; age int}{"Amy",18} ``` Let's see a complete example. + ```Go package main @@ -55,8 +56,8 @@ type person struct { age int } -// compare the age of two people, then return the older person and differences of age // struct is passed by value +// compare the age of two people, then return the older person and differences of age func Older(p1, p2 person) (person, int) { if p1.age > p2.age { return p1, p1.age - p2.age @@ -67,13 +68,8 @@ func Older(p1, p2 person) (person, int) { func main() { var tom person - // initialization tom.name, tom.age = "Tom", 18 - - // initialize two values by format "field:value" bob := person{age: 25, name: "Bob"} - - // initialize two values with order paul := person{"Paul", 43} tb_Older, tb_diff := Older(tom, bob) @@ -81,13 +77,10 @@ func main() { bp_Older, bp_diff := Older(bob, paul) fmt.Printf("Of %s and %s, %s is older by %d years\n", tom.name, bob.name, tb_Older.name, tb_diff) - fmt.Printf("Of %s and %s, %s is older by %d years\n", tom.name, paul.name, tp_Older.name, tp_diff) - fmt.Printf("Of %s and %s, %s is older by %d years\n", bob.name, paul.name, bp_Older.name, bp_diff) } - -``` +``` ### embedded fields in struct 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,7 +105,7 @@ type Student struct { } func main() { - // initialize a student + // instantiate and initialize a student mark := Student{Human{"Mark", 25, 120}, "Computer Science"} // access fields @@ -120,21 +113,20 @@ func main() { fmt.Println("His age is ", mark.age) fmt.Println("His weight is ", mark.weight) fmt.Println("His specialty is ", mark.specialty) - // modify notes + + // modify mark's specialty mark.specialty = "AI" fmt.Println("Mark changed his specialty") fmt.Println("His specialty is ", mark.specialty) - // modify age - fmt.Println("Mark become old") + + fmt.Println("Mark become old. He is not an athlete anymore") mark.age = 46 - fmt.Println("His age is", mark.age) - // modify weight - fmt.Println("Mark is not an athlet anymore") mark.weight += 60 + fmt.Println("His age is", mark.age) fmt.Println("His weight is", mark.weight) } -``` +``` ![](images/2.4.student_struct.png?raw=true) 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 ```Go mark.Human = Human{"Marcus", 55, 220} mark.Human.age -= 1 -``` +``` All the types in Go can be used as embedded fields. ```Go package main @@ -184,7 +176,7 @@ func main() { fmt.Println("Her preferred number is ", jane.int) } -``` +``` In the above example, we can see that all types can be embedded fields and we can use functions to operate on them. 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 { } type Employee struct { - Human // embedded field Human + Human specialty string phone string // phone in employee } func main() { Bob := Employee{Human{"Bob", 34, "777-444-XXXX"}, "Designer", "333-222"} + fmt.Println("Bob's work phone is:", Bob.phone) - // access phone field in Human fmt.Println("Bob's personal phone is:", Bob.Human.phone) } -``` +``` ## Links - [Directory](preface.md)