Go-Arrays and slices

Source: Internet
Author: User
Tags array length

One, array

Like most other languages, the Go Language array is also a fixed-length sequence with the same element type.

(1) Creation of arrays

There are 3 ways to create an array:

1) [Length]type

2) [Length]type{value1, value2, ..., Valuen}

3) [...] Type{value1, value2, ..., Valuen}

As follows:

Func test5 () {var arr1 [5]int32var arr2 [5]int32 = [5]int32{1, 2, 3, 4, 5}ARR3: = [5]int32{1, 2, 3, 4, 5}ARR4: = [5]int32{ 6, 7, 8, 9, 10}ARR5: = [...] Int32{11, 15}ARR6: = [4][4]int32{{1}, {1, 2}, {1, 2, 3}}fmt. Println (arr1) fmt. Println (ARR2) fmt. Println (ARR3) fmt. Println (ARR4) fmt. Println (ARR5) fmt. Println (ARR6)}

Output:

[0 0 0 0 0] [1 2 3 4 5] [1 2 3 4 5] [6 7 8 9 10] [11 12 13 14 15] [[1 0 0 0] [1 2 0 0] [1 2 3 0] [0 0 0 0]]

We look at the array arr1, only declare, not assigned, and the Go language automatically assigns us a value of 0. Looking again at arr2 and ARR3, we can see that the go Language Declaration, can indicate the type, can also not indicate the type, var arr3 = [5]int32{1, 2, 3, 4, 5} is also completely no problem.

(2) The capacity and length of the array are the same. Both the CAP () function and the Len () function output the capacity (that is, the length) of the array. Such as:
Func Test6 () {iarray4: = [5]int32{6, 7, 8, 9, 10}fmt. Println (Len (iarray4)) fmt. Println (Cap (IARRAY4))}

The output is all 5.

(3) Use:For Range
Func test7 () {arr: = [5]string{"aaa", ' BB ', "OK", "Tell me what to say", "()"}fmt. PRINTLN (arr) for I: = Range arr {fmt. Println (Arr[i])}}

Output:

[AAA BB can tell me what to say good ()]aaabb can tell me what to say good ()
Second, slicing

In the go language, slices are the same sequence of elements with variable lengths and fixed capacity. The slice nature of the go language is an array. The capacity is fixed because the length of the array is fixed, and the slice's capacity is the length of the hidden array. Variable length refers to the variable in the range of the array length.

(1) Creation of slices

There are 4 ways to create slices:

1) make ([]type, length, capacity)

2) make ([]type, length)

3) []type{}

4) []type{value1, value2, ..., Valuen}

From 3), 4), the only difference between creating a slice and creating an array is whether there is a number in the "[]" before Type, or a slice if it is empty, otherwise it represents the array. Because the slices are variable in length. Here's an example of creating a slice:

Func test8 () {Slice1: = make ([]int32, 5, 8) Slice2: = Make ([]int32, 9) Slice3: = []int32{}slice4: = []int32{1, 2, 3, 4, 5}FM T.println (Slice1) fmt. Println (SLICE2) fmt. Println (Slice3) fmt. Println (Slice4)}

Output:

[0 0 0 0 0] [0 0 0 0 0 0 0 0 0] [][1 2 3 4 5]

As above, created 4 slices, 3 empty slices, and one valued slice.

(2) slicing and hiding arrays

A slice is a reference to a hidden array, and a slice of that slice also references the same array. The following example creates a slice slice0 and creates 2 slices of slice1 and slice2 based on the slice:

Func Test9 () {slice0: = []string{"A", "B", "C", "D", "E"}slice1: = Slice0[2:len (SLICE0)-1]slice2: = slice0[:3]fmt. Println (Slice0, Slice1, slice2) slice2[2] = "8" FMT. Println (Slice0, Slice1, Slice2)}

Output:

[a b c D e] [C D] [a B c] [a B 8 d e] [8 d] [A B 8]

As you can see, slices slice0, Slice1, and Slice2 are references to the same underlying array, so SLICE2 changes and the other two change.

(3) traversing and modifying slices
Func test10 () {slice0: = []string{"A", "B", "C", "D", "E"}fmt. Println ("\n~~~~~~ element Traversal ~~~~~~") for _, Ele: = Range Slice0 {fmt. Print (Ele, "") Ele = "7"}fmt. PRINTLN ("\n~~~~~~ index traversal ~~~~~~") for index: = range Slice0 {fmt. Print (Slice0[index], "")}fmt. Println ("\n~~~~~~ element index common use ~~~~~~") for index, ele: = Range Slice0 {fmt. Print (Ele, Slice0[index], "")}fmt. PRINTLN ("\n~~~~~~ modified ~~~~~~") for index: = range Slice0 {Slice0[index] = "9"}fmt. Println (SLICE0)}

As above, the first three loops use a different for Range loop, when the for followed, the range has 2 elements in front of it, the element represents the index, the second element represents the value of the element, and "_" is ignored because the unused value in the Go language causes a compilation error.

When there is only one element, the element represents the index.

You can modify an element only by using an index. As in the first traversal, the assignment ele is 7, and the result has no effect. Because Ele is a value pass in an element traversal, Ele is a copy of the slice element, and modifying it does not affect the original value, but in the fourth traversal-the index traversal, the value referenced by the slice element is modified, so it can be modified.

The result is:

~~~~~~ element traversal ~~~~~~a b c d e ~~~~~~ index traversal ~~~~~~a b c d e ~~~~~~ element index common use ~~~~~~AA bb cc dd EE ~~~~~~ modify ~~~~~~[9 9 9 9 9]

(4), Append, copy slices

Func test11 () {slice: = []int32{}fmt. Printf ("The length of the slice:%d,slice:%v\n", Len (slice), slice) slice = append (slice, 9, one-, ten-,-one) fmt. Printf ("After appending, the length of the slice is:%d,slice:%v\n", Len (slice), slice) SLICECP: = Make ([]int32, (Len (slice))) fmt. Printf ("SLICECP Length:%D,SLICECP:%v\n", Len (SLICECP), SLICECP) copy (SLICECP, slice) fmt. Printf ("After copying the assignment, the length of the SLICECP is:%D,SLICECP:%v\n", Len (SLICECP), SLICECP)}

Append, copy slices, with built-in functions append and copy,copy functions return the number of elements that were last copied.

(5), built-in function append ()

Built-in function append () can append one or more other values of the same type to a slice. If the number of elements appended exceeds the original slice capacity, then the last returned is a new slice in a completely new array. If not, the last returned is a new slice in the original array. In any case, append () has no effect on the original slice. The following example:

Func test12 () {slice: = []int32{1, 2, 3, 4, 5, 6}slice2: = Slice[:2]_ = Append (Slice2,,, +) fmt. Printf ("Slice:%v\n", slice) fmt. Printf ("Operational slices:%v\n", slice2) _ = Append (Slice2, ()) fmt. Printf ("Slice:%v\n", slice) fmt. Printf ("Operation slices:%v\n", Slice2)}

As above, the append () method was used 2 times, resulting in a completely different result because the number of elements appended to the second append () method did not exceed the capacity of slice. In any case, the original slice slice2 has no effect. Results:

Slice is: [1 2 3 4 5 6] operation of the slice: [1 2]slice: [1 2 50 60 5 6] operation of the slice: [1 2]

Go-Arrays and slices

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.