Go language learning four: struct type

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

struct

We can declare a new type as a property or a field container for other types. For example, create a custom type that represents a person's entity. This entity has attributes: Name & age. Such a type we call a struct.
Type person struct{        name string age        int}

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

In addition to the above declaration of the use of P, there are two other ways to use the declaration of 1, in order to provide the initialization value
P: = person{"liuxinming", 28}

2, by the way of field:value initialization, so that can be arbitrary order
P: = person{age:28, Name: "Liuxinming"}

Example:
Package Mainimport "FMT"//declares a new type of person struct {name Stringavg  int}//compares two person's age, returns the older one, and returns the age difference//struct Also the value of the 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 well-written initialization bob: = person{avg:25, Name: "Bo B "}//to initialize Paul in the order of struct definitions: = 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:
Of Tom and Bob, Bob is older by 7 Yearsof Tom and Paul, Paul was older by Yearsof Bob and Paul, Paul was older by S

Anonymous fields for structs

We have described how to define a struct, defined by a field name corresponding to its type one by one, and in fact go supports providing only the type, not the way to write the field name, that is, anonymous fields, also known as embedded fields. When the anonymous field is a struct, all the fields owned by the struct are implicitly introduced to the currently defined struct.
Struct2.gopackage mainimport ("FMT") type Human struct {name   stringage    intweight int}type Student struct { Human      //anonymous field, the default struct contains Human all fields speciality String}func main () {//Initialize a student mark: = student{human{"Mark", 25, 100 }, "Computer science"}//access to the corresponding field FMT. Println ("His name is", Mark.name) fmt. Println ("His was", Mark.age) fmt. Println ("He weight is", mark.weight) fmt. Println ("He speciality is", mark.speciality)//modify the corresponding information mark.speciality = "AI" FMT. Println ("Mark changed his speciality") fmt. Println ("He speciality is", mark.speciality)//  Modify his age information fmt. Println ("Mark become old") Mark.age = 46fmt. Println ("He is", mark.age)//  modifies his weight information fmt. Println ("Mark is not a athlet anymore") mark.weight + = 60fmt. Println ("He weight is", mark.weight)}

Output Result:
His name was  Markhis age is  25His weight was  100His speciality is  computer Sciencemark changed he specialit YHis speciality is Aimark become oldhis age is 46Mark are not a athlet anymorehis 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.