Merge commit '61a6b4249047ad07e91e7791a03bdae45ed847ed' into ja

This commit is contained in:
Shin Kojima
2014-09-21 22:41:53 +09:00

View File

@@ -2,7 +2,7 @@
## struct
We can define new type of container of other properties or fields in Go like in other programming languages. For example, we can create a type called `person` to represent a person, this type has name and age. We call this kind of type as `struct`.
We can define new types of containers of other properties or fields in Go just like in other programming languages. For example, we can create a type called `person` to represent a person, with fields name and age. We call this kind of type a `struct`.
type person struct {
name string
@@ -25,21 +25,21 @@ Let's see how to use it.
var P person // p is person type
P.name = "Astaxie" // assign "Astaxie" to the filed 'name' of p
P.name = "Astaxie" // assign "Astaxie" to the field 'name' of p
P.age = 25 // assign 25 to field 'age' of p
fmt.Printf("The person's name is %s\n", P.name) // access field 'name' of p
There are three more ways to define struct.
There are three more ways to define a struct.
- Assign initial values by order
P := person{"Tom", 25}
- Use format `field:value` to initialize without order
- Use the format `field:value` to initialize the struct without order
P := person{age:24, name:"Bob"}
- Define a anonymous struct, then initialize it
- Define an anonymous struct, then initialize it
P := struct{name string; age int}{"Amy",18}
@@ -54,7 +54,7 @@ Let's see a complete example.
age int
}
// compare age of two people, return the older person and differences of age
// compare the age of two people, then return the older person and differences of age
// struct is passed by value
func Older(p1, p2 person) (person, int) {
if p1.age>p2.age {
@@ -88,9 +88,9 @@ Let's see a complete example.
### embedded fields in struct
I just introduced you how to define a struct with fields name and type. In fact, Go support fields without name but types, we call these embedded fields.
I've just introduced to you how to define a struct with field names and type.s In fact, Go supports fields without names, but with types. We call these embedded fields.
When the embedded field is a struct, all the fields in that struct will be the fields in the new struct implicitly.
When the embedded field is a struct, all the fields in that struct will implicitly be the fields in the struct in which it has been embdedded.
Let's see one example.
@@ -135,12 +135,12 @@ Let's see one example.
Figure 2.7 Inheritance in Student and Human
We see that we access age and name in Student just like we access them in Human. This is how embedded field works. It is very cool, isn't it? Hold on, there is something cooler! You can even use Student to access Human this embedded field!
We see that we can access the age and name fields in Student just like we can in Human. This is how embedded fields work. It's very cool, isn't it? Hold on, there's something cooler! You can even use Student to access Human in this embedded field!
mark.Human = Human{"Marcus", 55, 220}
mark.Human.age -= 1
All the types can be used as embedded fields.
All the types in Go can be used as embedded fields.
package main
import "fmt"
@@ -179,11 +179,11 @@ All the types can be used as embedded fields.
fmt.Println("Her preferred number is", jane.int)
}
In above example we can see that all types can be embedded fields and we can use functions to operate them.
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, if Human has a field called `phone` and Student has a field with same name, what should we do?
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?
Go use a very simple way to solve it. The outer fields get upper access levels, which means when you access `student.phone`, we will get the field called phone in student, not in the Human struct. This feature can be simply seen as `overload` of field.
Go use a very simple way to solve it. The outer fields get upper access levels, which means when you access `student.phone`, we will get the field called phone in student, not the one in the Human struct. This feature can be simply seen as field `overload`ing.
package main
import "fmt"