Go Golang type of struct

Source: Internet
Author: User

http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=22312037&id=3756923

First, struct
In the go language, there are structs, which are similar to the C language, as illustrated below:
Type person struct {
Name string
Age int
}
The above declares a struct person, which contains two fields. You can use this struct as follows:
var P person//p is now a variable of type person
P.name = "SHICQ"//assignment to P's Name property
P.age = 31//Assign to P's age property
Fmt. Printf ("The person's name is%s", p.name)//Access P's Name property
In addition to the use of the above statement of P, there are two ways of declaring it:
(1) Provide initialization values in order.
P: = Person ("Li Lei", 25)
(2) initialized by Field:value, so that it can be in any order.
P: = Person (age:24, Name: "Han Meimei")

Second, anonymous fields
The previous article describes how to define a struct, which is defined by the field name corresponding to its type one by one, in fact, the go language support only provides the type, but does not write the name of the way, that is, anonymous fields, or called embedded fields. When the anonymous field is a struct, all the fields owned by the struct are implicitly introduced to the currently defined struct. The following examples illustrate:
Type Human struct {
Name string
Age int
}
Type Student struct {
Human//Anonymous field, then the default student contains all the fields of Human
Speciality string
}
Initialize Student
Mark: = Student (human{"Shicq", +}, "Computer science")
Access the appropriate fields
Fmt. Println ("His name is", Mark.name)
Fmt. Println ("He is", mark.age)
Fmt. Println ("He speciality is", mark.speciality)
When we see student accessing the attribute age and name, it's like accessing the fields that you own. Of course student can also access these two fields by accessing human:
Mark. Human = human{"Shicq", 31}
Mark. Human.age-= 1
Not just a struct field, all built-in types and custom types can be used as anonymous fields, such as slice.
What if there is a field called phone in human, and student has a field called phone?
The outermost priority access in the Go language, which is when accessed through Student.phone, is the field inside the student, not the field inside the human.
This allows us to overload some of the fields that are inherited through anonymous fields, and if you want to access the fields in the corresponding anonymous type after the overload, you can visit by anonymous field name:
Type Human struct {
Name string
Age int
Phone string//human fields owned by type
}
Type Employee struct {
Human//anonymous field Human
Speciality string
Phone string//employee's phone field
}
Bob: = employee{human{"Bob", "777-444-xxxx"}, "Designer", "333-222"}
Fmt. Println ("Bob's work phone is", Bob.phone)
Fmt. Println ("Bob's personal phone is", Bob.Human.phone)//Access Human's phone field




Go Golang type of struct

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.