Go-struct type

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

1. Overview
Like the C language struct, similar to class java/php, in Go, for extended types, object-oriented programming (this section is not described in detail), etc.
Declaration format: (is a combination of types)
  Type typeName struct {    //...  }

2. Declaration and initialization

There are several methods:
  var varName typeName             //①  varName: = new (TypeName)         //②  varName: = typename{[initialization value]}  //③  VarName: = &typename{[initialization value]}//④
Note: ①③ returns the typeName type variable; ②④ returns the *typename type variable; ③④[] can be omitted; if there is no initialization value, the default is zero value
There are two types of initialization values:
a. Ordered: typename{value1, value2, ...} must be one by one corresponding
b. Unordered: typename{field1:value1, Field2:value2, ...} to initialize partial values
Cases:
  Type person struct {    name string age    int  }  P: = person{"James", +/  /ordered  p: = person{age:23}       //unordered
3. Operation
The declared struct is the same as the normal type
Access a variable name in the struct, using "." To connect:
Varname.field
If you are manipulating Age:p.age = 35 in the person structure above
It can also be used as a parameter in a function to return a value type

Such as:

Func funcName (varName1 typename2[,varname2 typeName2, ...]) typeName {...}

Example code list

Package Mainimport "FMT"//1. Declares a struct with a custom type named person, type A, struct {name stringage int}func main () {//2. initialize var p1 Personp2: = person{}p3: = Person {"James", 23}p4: = person{age:23}fmt. Println (P1, p2, p3, p4) P5: = new (person) P6: = &person{}p7: = &person{"James", 23}p8: = &person{age:23}fmt. Println (P5, P6, P7, P8)/*********************************//*print result                   *//*{0} {0} {James $} {23°c}     *//*& {0} &{0} &{james} &{23} *//*********************************///3. Operation P1.age = 50p2.age = 25if compareage (p1, p2) {fmt. Println ("P1 is older than P2")} else {fmt. Println ("P2 is older than P1")}/*********************************//*print result                   *//*p1 is older than P2            *//*** /}func Compareage (p1, p2 person) bool {if p1.age > p2.age {return True}return false}

4. Anonymous fields

Declares that a struct1 can contain an existing struct2 or go language built-in type as a built-in field called an anonymous field, that is, write only TypeName, no varName, but TypeName cannot repeat
Inheritance in anonymous fields and object-oriented programming languages
4.1 Declaration and initialization:
Such as:
  Type person struct {    name string age    int    addr String  }  type Employee struct {    person          // anonymous field    salary int    int             //with built-in type as Anonymous field    addr string     //similar to overload  }  em1: = employee{person{ "Rain", Max, "Qingyangqu"}, "Gaoxingqu"}
4.2 Operation
Access is also via "." To connect
The same field takes the outermost first access, similar to the overloaded
EM1.ADDR access is the outermost addr of the Employee
Em1. PERSON.ADDR access is the addr of the person in the Employee


Example code list

Package Mainimport ' FMT ' type person struct {name stringage intaddr string}type Employee struct {person//anonymous field Sala Ry Intint//Use built-in type as anonymous field addr string//similar to Overload}func Main () {/*var em1 Employee = employee{}em1. person = person{"Rain", "qingyangqu"}em1.salary = 5000em1.int = 100//Notice the meaning when used, there is no em1.addr = "Gaoxingqu" *///em1: = Emp loyee{person{"Rain", "Max", "Qingyangqu"}, "Gaoxingqu"}//initialization method is different, but the result is the same em1: = employee{person:person{"Rain", "Qingyangqu"}, salary:5000, int:100, addr: "Gaoxingqu"}fmt.                                        Println (EM1)/******************************************************//*print result *//*{{rain Qingyangqu} Gaoxingqu} *//******************************************************/fmt. Println ("Live addr (em1.addr) =", em1.addr) fmt. Println ("Work addr" (Em1. PERSON.ADDR) = ", em1.                              PERSON.ADDR) Em1.int = 200//Modify the value of the anonymous field/******************************************************//*print result          *//*live addr (em1.addr) = Gaoxingqu *//*work addr (em1. PERSON.ADDR) = Qingyangqu *//******************************************************/}


There's a lot of knowledge about struct object-oriented, and then we'll learn later
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.