Go language arrays and slicing examples

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

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 arrays: [Length]type, [N]type{value1, value2, ..., valuen}, [...] Type{value1, value2, ..., valuen} are as follows:

Copy the code code as follows:

Func Test5 () {
var iarray1 [5]int32
var iarray2 [5]int32 = [5]int32{1, 2, 3, 4, 5}
Iarray3: = [5]int32{1, 2, 3, 4, 5}
Iarray4: = [5]int32{6, 7, 8, 9, 10}
Iarray5: = [...] Int32{11, 12, 13, 14, 15}
Iarray6: = [4][4]int32{{1}, {1, 2}, {1, 2, 3}}
Fmt. Println (Iarray1)
Fmt. Println (Iarray2)
Fmt. Println (IARRAY3)
Fmt. Println (IARRAY4)
Fmt. Println (Iarray5)
Fmt. Println (IARRAY6)
}

Results:

[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 iarray1, only declare, not assigned, and the Go language automatically assigns us a value of 0. Looking again at Iarray2 and Iarray3, we can see that the go Language Declaration, can indicate the type, can also not indicate the type, var iarray3 = [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:

Copy the code code as follows:

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:

Copy the code code as follows:

Func test7 () {
Iarray7: = [5]string{"aaa", ' BB ', "yes", "tell me what to say", "()"}
Fmt. Println (IARRAY7)
For I: = Range Iarray7 {
Fmt. Println (Iarray7[i])
}
}

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:

Copy the code code as follows:

Func Test8 () {
Slice1: = Make ([]int32, 5, 8)
Slice2: = Make ([]int32, 9)
Slice3: = []int32{}
Slice4: = []int32{1, 2, 3, 4, 5}
Fmt. Println (Slice1)
Fmt. Println (SLICE2)
Fmt. Println (SLICE3)
Fmt. Println (Slice4)
}

The output is:

[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) slices and hidden 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:

Copy the code code as follows:

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)
}

The output is:

[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:

Copy the code code as follows:

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~~~~~~ modify ~~~~~~")
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
~~~~~~ modifying ~~~~~~
[9 9 9 9 9]

(4), Append, copy slices:

Copy the code code as follows:

Func test11 () {
Slice: = []int32{}
Fmt. Printf ("Slice length:%d,slice:%v\n", Len (slice), slice)
Slice = append (slice, 12, 11, 10, 9)
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 you 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, the append has no effect on the original slice. The following example:

Copy the code code as follows:

Func test12 () {
Slice: = []int32{1, 2, 3, 4, 5, 6}
Slice2: = Slice[:2]
_ = Append (Slice2, 50, 60, 70, 80, 90)
Fmt. Printf ("Slice:%v\n", slice)
Fmt. Printf ("Operational slices:%v\n", Slice2)
_ = Append (Slice2, 50, 60)
Fmt. Printf ("Slice:%v\n", slice)
Fmt. Printf ("Operational 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: [1 2 3 4 5 6]
Slices of operation: [1 2]
Slice: [1 2 50 60 5 6]
Slices of operation: [1 2]

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.