"Go Language" "12" Go language structure

Source: Internet
Author: User

When I first came into contact with the C language, I put a great deal of interest in the structure, think that this thing in the future, and then contact Java, C + +, object-oriented programming objects into my sight, after so many years of discipline, back to look at the structure is still so kind At the same time, from another perspective, how similar the struct is to the object-oriented member object:)

I. Structural elements

Structs consist of keywords, struct type names, and specific members, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/61/wKiom1WaZv2RbnY3AAC_DN43xqI956.jpg "title=" struct definition. png "alt=" wkiom1wazv2rbny3aac_dn43xqi956.jpg "/>


Ii. Preliminary understanding of structure

Here's a look at the structure by contrasting arrays (composite types):

1, from the storage type view

Arrays can only store the same type:

s: = []string{"A", "B", "C", "D", "E"}

Structs can store different types

Type Employee struct{
Name, address string//name, address
Age int//ages
Height,weight float64//height, weight
}


2. From memory view

They all occupy contiguous memory space in memory, but for arrays, each element consumes the same amount of memory, and the size of each item in the struct is not necessarily the same.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/5F/wKioL1WacC7ARF99AAB2IrtGm7Q976.jpg "title=" Memory angle. png "alt=" wkiol1wacc7arf99aab2irtgm7q976.jpg "/>


3. From a type combination perspective

Arrays have no combination of usages, such as a one-dimensional array, and once the array type is determined it is no longer possible to set another one-dimensional array to the element value, for example

s: = []string{"A", "B", "C", "D", "E"}

S[0] = []string{"F", "G"}

Running the program at this point will appear similar to this prompt:cannot use []string literal (Type []string) as type string in assignment;

Structure support combination, we know that one-dimensional space is a line, two-dimensional space is a plane, three-dimensional space is a space

Type line struct {
x int
}

Type plane struct {
Line
y int
}

Type space struct {
Plane
z int
}

It is natural for us to extend one dimension to two dimensions, to extend the two dimensions to three dimensional, to extend the three-dimensional to four-dimensional, and so on and so on, by way of combination.


4, from the operational point of view

The operation of an array element is done by subscript:

s: = []string{"A", "B", "C", "D", "E"}
For I: = 0; I < Len (s); i++ {
Fmt. Println (S[i])//print each element of an array, by s[i] subscript the way to get
}

The structure is accomplished by the item name:

T: = Space{plane{line{3}, 5}, 7}
Fmt. Println (T.x, T.Y, t.z)//By manipulating the structure's item name T.x, T.Y, t.z to get


5. From a comparative point of view

Arrays are similar to structs, if you want to determine whether the two arrays are the same, you need to look at the storage type of the array, the length of the array, whether each element is equal, the same to determine whether the two structures are the same, you need to see the structure of the same type, and then see the order of items, the name of the item,


Third, the initialization of structural body

The initialization of the array can be found in the "6" Go language array, the relative structure of the initialization of the array is a bit complicated, the following one by one way to:

1, empty structure body

The so-called empty struct, the member of the struct, is empty, as follows:

Type employee struct {
}

Func Main () {
EMP: = employee{}//initialization of struct body, directly using struct type name followed by a curly brace
Fmt. PRINTLN (EMP)
}

where employee{} represents the initialization of a struct, and then assigns to the EMP, the run will print the result {}, because the struct member is empty; maybe some readers think, if the struct has a member, the same is the result of initialization?

Type employee struct {
Name, address string//name, address
Age int//ages
Height, weight float64//height, weight
}

Func Main () {
EMP: = employee{}//So what's the result?
Fmt. PRINTLN (EMP)
}

Running it will find that the result is {0 0 0}, because the default value of the string is empty, is not displayed, and the default value of int and float64 is 0, so the result is printed. In fact, this is the default value of struct members, as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/5F/wKioL1WafAnhdRBGAABTJEO04ow570.jpg "title=" struct default value. png "alt=" wkiol1wafanhdrbgaabtjeo04ow570.jpg "/>


2, the initialization of the structure body

Member initialization of a struct is done by manipulating the member object.

Func Main () {
EMP: = employee{}
Fmt. PRINTLN (EMP)

Emp.name = "Hacker Eagle"
Emp.age = 38
Fmt. PRINTLN (EMP)
}

Adopt variable + "." + member name = value The form of the struct is initialized, such as emp.age=38. This type of initialization is similar to C + + and Java, so is there any other form? Of course, previously said go language is the normal language of human thinking, as long as you can think, basically can be normal execution:)

EMP1: = employee{"Keji", "Hangzhou", 19, 175, 65}
Fmt. Println (EMP1)

This form of initialization looks more intuitive. The results of the above implementation are as follows:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/5F/wKioL1WafuWiLtDKAABqafzO-4U073.jpg "title=" struct initialization. png "alt=" wkiol1wafuwiltdkaabqafzo-4u073.jpg "/>

The reader may also ask, I just want to assign a value to some of these members, and the above is to assign values to all the members.

EMP2: = employee{address: "Hangzhou", age:20}
Fmt. Println (EMP2)

This allows only the assigned member to get the true value, and the member that does not specify the assignment is given the default by the system, which is also known as the literal value class.


3, nested structure body

This is a good understanding, that is, the structure of nested structures, we define "height", "weight" as a structure, and "height", "weight" is a human (struct) members, so you can use nested structure:

Shape
Type Figure struct {
Height, weight float64
}

Type human struct {
Name, address string
Figure
}

That is, the human structure contains the figure structure, we can use the following initialization

Man: = human{}

Fmt. Println (man)

The execution result is {{0 0}}


In conjunction with the above-mentioned structure initialization, it is easy to initialize the name and address by literal value.

Man.name = "Siyu"

man.address = "Tianjin"

But how to initialize the nested struct member height, weight? People who have used object-oriented programming can easily think of the following way:

Func Main () {
Man: = human{}
Man.name = "Siyu"
man.address = "Tianjin"

Man.figure.height = 172.8
Man.figure.weight = 175.3
Fmt. Println (man)
}

That is, a layer down to look for: First man's member figure, and then through the figure of the member height of the weight of the assignment, so there is no problem, in fact, go gives us a more convenient way to assign the value:

Man.height = 172.8
Man.weight = 175.3

It is straightforward to assign values directly to its members, but introduces the concept of "member visibility"


4. Visibility of struct members

Any language in front of the code is pale, agile has a thought is that the code is more than the document, crap less, in code to explain what is "struct member visibility"

Life will laugh and cry, so there are members crying and laughing.
Type biology struct {
Cry, laugh string
}

People laugh and cry, so there are members who cry and laugh, but they also have nested biological structures.
Type human struct {
Biology
Cry, laugh string
}

The following methods are used to initialize a struct

Man: = human{}

Man.cry = "Cry"

Man.laugh = "Laugh"

Fmt. Println (man)

So the man.cry,man.laugh here is to assign values to human's members? or assign a value to a member of the biology? Run the results and you'll know that this is a member assignment to human.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/5F/wKioL1WahjbCH1-XAABJlgjXV78216.jpg "title=" nested struct initialization. png "alt=" wkiol1wahjbch1-xaabjlgjxv78216.jpg "/>

Because the inner curly braces are empty, why is this?

can be peeled onion thinking to understand, if the outermost has this member name (Cry, laugh) will not be stripped to the inside, if the outermost layer does not have the member name, then further to the inside stripping, until found;

That is, if the outer layer has a member name (Cry, laugh), then the inner member of the same name is not visible, if the outer layer does not have a member name (Cry, laugh), the members of the inner layers will become invisible


5, again discuss the initialization of nested structure body

For example, we can initialize in literal form:

Man: = human{}

Man.cry = "Cry"

Man.laugh = "Laugh"

Man.biology.cry = "Biology Cry"

Man.biology.laugh = "Biology Laugh"

You can actually use the following form:

Woman: = human{biology:biology{cry: "Biology Cry", Laugh: "Biology Laugh"}, cry: "Cry", laugh: "Lau GH "}

It can also be simplified to:

Woman: = human{biology:biology{"Biology Cry", "Biology Laugh"}, cry: "Cry", laugh: "Laugh"}

Can it be simplified as well?

Woman: = human{{"Biology Cry", "Biology Laugh"}, "Cry", "Laugh"}

At this point, the following exception is thrown:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/62/wKiom1WaiYqAE2bqAACJITQRi6Y131.jpg "title=" Nested struct initialization 2.png "alt=" Wkiom1waiyqae2bqaacjitqri6y131.jpg "/>

If you are careful enough, you can find that the nested struct only writes a struct type name, not the form of value ValueType , so for this case, the go language considers the internal nested struct name and type name to be the same

This article is from the "Green Guest" blog, please be sure to keep this source http://qingkechina.blog.51cto.com/5552198/1671463

"Go Language" "12" Go language structure

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.