Go worker layer-based tutorials: array, slice, map

Source: Internet
Author: User
Here we are going to parse data sets, slices, and maps. Maybe you are writing to go from other languages, in other languages, you may not be very familiar with the Data Group of Go statement, because go statement splits the data group into array, slice, and map, let's take a look at the difference between the three and their usage in the next time. first, let's talk about the array part:
1. The length of a Data Group depends on a part of the Data Group. Let's take a look at the example below.
A: = [1] int {} and B: = [2] int {}
Both A and B are data groups, but they have different durations. In the go statement, they think they are two different types. Since they are different types, then they cannot perform operations that compare values or directly limit values. in fact, this is not easy to understand. Because the go statement is a statement of type-based security, it is only necessary to modify the variable type of the parameter value that spans the type.
2. In the go statement, data is grouped into a value type rather than a reference type. To obtain a data set, you only need to do so.
A: = [1] int {}; P: = &
You can obtain the group's metric using the previous sentence, and assign the value of the specified metric value to the variable p.
3. the struct name of array can only be an integer or struct type, and the lower Mark starts from scratch.

The Definition Format of array is:
VaR data group variable name = [length] type {}

Next let's take a look at this generation:
A1: = [2] int {} // specify a data group. The value of each element in the Data Group is the initial value of Int. A2: = [2] int {} // specify a Data Group and assign the initial value A3: = [2] int {} // specify a Data Group, give [1] The initial value of FMT. println (A1); FMT. println (A2); FMT. println (A3); P1: = & A1 // the FMT that gets the value of A1. println (P1); B: = [3] string {"AA", "BB", "cc"} // specify a Data Group of the string type, and subscribe to the initial value C: = [...] string {"AA", "BB", "cc"} // specify a data group. The length is not set, tell go in three cases to calculate the data group length FMT. println (B); FMT. println (c); FMT. println (B = C); // compare whether the two data groups are the same. C [1] = "BB" // modify the value FMT of [1. println (B = C); // whether the two data groups are the same as FMT. println (CAP (c); // gets the Data Group Capacity FMT. println (LEN (c); // obtain the length of the data set. // The array name must be a number or word. D: = [...] int {96: 3, 'A': 4} // use the character as the alias name, because each character corresponds to an int-type sequence, the actual sequence name is also an int-type FMT. println (d); E: = [2] [3] int {, 1}, {, 2} // specify a binary data group FMT. println (E); F: = [...] [3] int {, 1}, {, 2} // specify a multi-dataset. The numeric value allows go statements to calculate FMT. println (f); P: = new ([4] INT) // This is another explicit data group method. You can use the new keyword, however, this explicit method returns the pointer FMT pointing to the Data Group. println (P );



Next let's take a look at slice slices. The Slice slices need to know the following requirements:
1. The Slice itself is not a data group, but its bottom layer points to other data groups.
2. Slice can be used to extend the feature of the variable length. We know that the length of the array is fixed.
3. Slice is a reference type. We know that array is a value type.
4. Use make to create a slice, and use new to create an array.
5. if multiple slices point to the same data group, when a value of this data group is changed, the values of all slices are the same as those of this group, this is the feature of the reference type.

The format of the partition method is as follows:
Slice name = make ([] data group name, length, capacity)

Let's take a look at the usage of Slice:
// S = make ([] int, 3, 5) // This is a quasi-defining method. The content is allocated at the same time when the slicing is fixed. // FMT. println (s); A: = [...] int {1, 2, 3, 4, 5, 6, 7, 8, 9, 0} FMT. println (a); s: = [] int {} // define a slice FMT. println (s); s = A [5:] // Let the slice point to Data Group A, with its initial value FMT. println (s); X: = A [4: 9] FMT. println ("the value of X is:", x); A [7] = 99 // change a value of the Data Group FMT. println (s); // print the slice and the FMT value also changes. println ("x value:", x); // at this time, the slice value is also changed to S = A [:] // get all the elements FMT. println (s );



Finally, let's take a look at the usage of map. MAP is different from a number or segment. Let's take a look at the differences.
1. Map uses the key-Value Method to store values. The key value is no longer limited to the int type and can be a string value.
2. MAP is much slower than a data group or slice. Therefore, if you can use a data group or slice, do not use map.
3. Map and slice are built using make. This is different from the new data set.
4. Map uses Delete to delete an element.
5. Use range to iterate the map or slice.

Next, let's take a look at the basic usage of MAP:
VaR M1 = map [String] float64 {"Bell Labs": 40.68433, "Google": 37.42202} m1 ["new"] = 1.01 // added the limit value to FMT. println ("three elements:", M1) Delete (M1, "new") FMT. println ("two elements:", M1) FMT. println (M1); // when ["new"] does not exist for K, V: = range M1 {// iterate over map FMT. printf ("M1 [% s] = % F \ n", K, v)} FMT. printf ("the length of M1 is % d \ n", Len (M1) M: = make (Map [String] INT) // use make to create map M ["K1"] = 7 m ["K2"] = 13 FMT. println ("map:", m)


In this way, we can see that array, slice, and map have different usage, and they also have poor performance. in daily use, we suggest using array and slice, however, map cannot be more active, but its efficiency is not high.


I hope that my friends can leave an article out, http://blog.csdn.net is grateful here!

I have set up a QQ group. If you are interested in commenting on the go statement, you can join: 195112

Go worker layer-based tutorials: array, slice, map

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.