Go language struct type introduction _golang

Source: Internet
Author: User

struct

We can declare a new type as another type of property or field container.
For example, create a custom type person to represent an entity of one. This entity has attributes: Name & age. This type is what we call struct.

Copy Code code as follows:

Type Person struct{
Name string
Age int
}

Copy Code code as follows:

var p person//P is now the person type variable.
P.name = "Astaxie"///Assign "Astaxie" to P's Name property.
P.age = 25//Assign "25" to the age attribute of the variable P
Fmt. Printf ("The person's name is%s", p.name)//access the Name property of P.


In addition to the use of the above P declaration, there are two other ways to use the Declaration

1. Provide initialization value in order

Copy Code code as follows:

P: = person{"liuxinming", 28}

2, through the Field:value method initialization, this can be any order

Copy Code code as follows:

P: = person{age:28, Name: "Liuxinming"}

Example:

Copy Code code as follows:

Package Main

Import "FMT"

Declaring a new type
Type person struct {
Name string
AVG int
}

Compares the age of two persons, returns the older person, and returns the age difference
Struct is also a passing value.
Func older (P1, p2 person) (person, int) {
If P1.avg > P2.avg {//compare P1 and P2 age
return P1, P1.avg-p2.avg
}
return P2, P2.avg-p1.avg
}

Func Main () {
var Tom person
Assignment initialization
Tom.name, Tom.avg = "Tom", 18

Two fields are clearly written and initialized
Bob: = person{avg:25, Name: "Bob"}

Class in struct definition order
PAUL: = person{"Paul", 43}

Tb_older, Tb_diff: = older (Tom, Bob)
Tp_older, Tp_diff: = older (Tom, Paul)
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)
}

The output results are as follows:

Copy Code code as follows:

of Tom and Bob, Bob is older by 7 years
Tom and Paul, Paul is older by years
of Bob and Paul, Paul is older by years

Anonymous fields for struct

We described above how to define a struct, defined as a field name corresponding to its type one by one, and actually go supports only the type, not the name of the write segment, which is the anonymous field, also known as the embedded field.

When an anonymous field is a struct, all the fields owned by the struct are implicitly introduced into the currently defined struct:

Copy Code code as follows:

Struct2.go
Package Main

Import (
"FMT"
)

Type Human struct {
Name string
Age int
Weight int
}
Type Student struct {
Human//Anonymous field, then the default struct contains all the Human fields
Speciality string
}

Func Main () {
Initialize a student
Mark: = student{human{"Mark", "N", "Computer Science"}

Access the appropriate fields
Fmt. Println ("His name is", Mark.name)
Fmt. Println ("his", Mark.age)
Fmt. Println ("His weight is", mark.weight)
Fmt. Println ("His speciality is", mark.speciality)

Modify the corresponding information
mark.speciality = "AI"
Fmt. Println ("Mark changed his speciality")
Fmt. Println ("His speciality is", mark.speciality)
Revise his age information.
Fmt. Println ("Mark become old")
Mark.age = 46
Fmt. Println ("his", Mark.age)
Revise his weight information.
Fmt. Println ("Mark is not a athlet anymore")
Mark.weight + 60
Fmt. Println ("His weight is", mark.weight)
}

Output results:

Copy Code code as follows:

His name is Mark
His 25
His weight is 100
His speciality are Computer science
Mark changed his speciality
His speciality is AI
Mark become old
His 46
Mark is isn't an athlet anymore
His weight is 160

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.