Go language learning notes (4) [array, slices, MAP]

Source: Internet
Author: User
Tags array example
Date: February 1, July 22, 2014. array [array] 1. Definition: array is defined by [N] <type>. N indicates the length of array, <type> indicates the type of the content to be stored. For example: var arr [10] int arr [0] = 1 arr [1] = 2: assign an array to another array and copy all the elements. In addition, when an array is passed to the function, it will get a copy of the array instead of the array pointer. 2. Compound declaration of arrays. A: = [3] int {1, 2, 3} or abbreviated as a: = [...] int {, 3} (go will automatically count the number of elements) 3. Multi-dimensional array example: A: = [2] [2] int {[2] int }, [2] int {3, 4} is equivalent to a: = [2] [2] int {[...] int {1, 2}, [...] int {3, 4} in the release, when the declared array element types are consistent, the internal element types can be omitted. The preceding Declaration can be changed to a: = [2] [2] int {1, 2}, {3, 4} 4. The array length is also part of the type, therefore, arrays with different lengths have different types. Ii. Slice [Slice] 1. Slice is close to array, but the length can be increased when new elements are added. Slice always points to the underlying array. Slice itself is not an array. Slice is a pointer to an array, which is different from array. Slice is a reference type, which means that when you assign a value to another slice, the two references point to the same array.

2. Create an slice: SL: = make ([] <type>, Len, Cap). The cap value can be omitted (when omitted, the value is equivalent to Len ), len indicates the number of elements to store, and cap indicates the capacity. Capacity size Cap = <real_cap <2 * cap. When the capacity cap is exceeded, the capacity will be automatically upgraded. However, it must be noted that after the capacity is automatically upgradedReallocationConsecutive memory addresses.

3. A Server Load balancer can be created in the form of a [I: J]. The new Server Load balancer points to a, starting from I to ending with (J-1) with a length of = J-I. 4. Slice append operation. Slice can append an element at the end, or even append an slice to the end of another slice. If the final length does not exceed the original slice, The append operation will return the original slice, otherwise, the memory address will be reassigned. 5. copy operation of slice. The copy operation returns the number of copied elements. The number of copied elements is the smallest value in Len (SRC) and Len (DST. 6. Slice operation examples
A: = [6] int {,} // array subscript starts from 0 S1: = A [] //, S2: = A [:] //, is the abbreviation of a [0: Len (a)] S3: = A [: 3] //, 3, is the abbreviation of a [0: 3] // S4: = A [] // array out of bounds, throwing invalid slice index 7 (out of bounds for 6-element array) S5: = S2 [:] //,. At this time, S5 and S2 still point to a. You can use the built-in function println (slice) to print the result comparison. S6: = [] int {1} S7: = append (S6, 2) S8: = append (s7, 2, 2, 3) // S9: = append (s7, S6 ...) // Note that there are three following points. If this parameter is not added, a type error is returned. I thought it was a ellipsis, and I had to spend a long time. VaR AR = [...] int {1, 2, 3, 5, 6, 7} N1: = copy (S10, ar [0:]) // return result n1 = 6, in this case, the S10 value is [1 2 3 5 6 7] N2: = copy (S10, S10 [2:]) // The returned result n2 = 4, in this case, the S10 value is [3, 5, 6, 7, 7]. The problem about slice capacity resizing and re-allocating the memory address is not completely clear. Please add it later.
3. Map [dictionary] 1. map exists as a built-in type in the go language. The general method for defining map is: Map [<from type>] <to type> For example: Sexes: = map [String] string {"man": "male ", "male": "female", // the comma following must be written.} 2. Declare map: mapname: = make (Map [type1] type2) {} 3. Map Index value, mapname ["key"], for example, sexes ["male"] 4. Traverse map, you can use range, for example: for _, sex: = range sexes {FMT. println (sex)} 5. Add the element mapname ["newkey"] = value to the map, for example: sexes ["unknow"] = "unknown gender" // Add element 6 to map. Delete the map element: delete (mapname, "key"), for example: delete (sexes, "unknow") // deletes the elements in the map, and deletes the instances created by M [x] In the map. 7. checks whether an element exists in the map, for example: var val string var present bool Val, present = sexes ["nothing"] can also be directly in the form of "comma OK", such as: V, OK: = sexes ["male"]
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.