Go language Array and slicing instance detailed _golang

Source: Internet
Author: User

This example describes the use of the Go Language array and slices. Share to everyone for your reference. The specific analysis is as follows:

One, array 

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

(1) The creation of the array.

There are 3 ways to create an array: [Length]type, [N]type{value1, value2, ..., valuen}, [...] Type{value1, value2, ..., valuen} are as follows:

Copy 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, just declare that it's not assigned, and the Go language automatically assigns a value of 0. Looking at Iarray2 and iarray3, we can see that the Go Language declaration, which can indicate the type, or not the type, var iarray3 = [5]int32{1, 2, 3, 4, 5} is completely OK.

(2) the size and length of the array are the same. The CAP () function and the Len () function output the capacity (that is, the length) of the array. such as:

Copy 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 5.

(3) Use:

Copy Code code as follows:
Func test7 () {
Iarray7: = [5]string{"aaa", ' BB ', "OK", "Tell me what to say", "()"}
Fmt. Println (IARRAY7)
For I: = Range Iarray7 {
Fmt. Println (Iarray7[i])
}
}

Second, slice

In the go language, slices are the same sequence of elements with variable length 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 size of the slice hides the length of the array. Variable length refers to a variable in the range of the length of the array.

(1) The 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}

Visible from 3, 4), the only difference between creating a slice and creating an array is whether there are numbers in [] before the Type, which is empty, or a slice, otherwise it represents an array. Because slices are variable in length. The following is an example of creating a slice:

Copy 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, a slice with a value.

(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 Slice1 and slice2 from this slice:

Copy 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]
Visible, slices Slice0, Slice1, and Slice2 are references to the same underlying array, so SLICE2 changed, and the other two change.

(3) Traversing, modifying slices:

Copy 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, and when a for is followed by a 2 element in the range, the second element represents the index, the other represents the element value, and "_" is ignored, because unused values in the Go Language cause compilation errors.

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

An element can be modified only by indexing. As in the first traversal, the assignment ele is 7, and the result has no effect. Because in the element traversal, the ele is the value pass, ele is the slice element's copy, modifies it does not affect the original value, but in the fourth traversal--the index traversal, modifies is the slice element reference value, therefore may modify.

The results are:

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

Copy 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 ("Append, slice Length:%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 the copy 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 functions append can append one or more other values of the same type to a slice. If the number of additional elements exceeds the original slice capacity, then the final return is a new slice in a completely new array. If not, the final return is a new slice in the original array. In any case, append has no effect on the original slice. The following example:

Copy 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 ("Slice of Action:%v\n", Slice2)
_ = Append (Slice2, 50, 60)
Fmt. Printf ("Slice:%v\n", slice)
Fmt. Printf ("Slice of Action:%v\n", Slice2)
}

As above, the Append method is used 2 times, resulting in a completely different result because the second Append method does not append more elements than the slice capacity. No matter what, the original slice slice2 have 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]

Hopefully this article will help you with your go language program.

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.