# GO language arrays and slicing examples

• 2020-05-07 19:53:19
• OfStack

This article illustrates the use of arrays and slices in the GO language. Share with you for your reference. Specific analysis is as follows:

1. Array

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

(1) array creation.

There are 3 ways to create an array: [length]Type, [N]Type{value1, value2... valueN}, [...]. Type {value1, value2,... , valueN} is 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]]

So if we look at the array iarray1, we just declare, we don't assign, Go automatically assigns us 0. Looking at iarray2 and iarray3, we can see that the declaration of Go language can indicate the type or not, var iarray3 = [5]int32{1, 2, 3, 4, 5} is also perfectly fine.

The size and length of the (2) array are one. Both the cap() and len() functions output the capacity (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 5.

(3) use:

``````func test7() {
iarray7 := [5]string{"aaa", `bb`, " You can! ", " What can I say ", "()"}
fmt.Println(iarray7)
for i := range iarray7 {
fmt.Println(iarray7[i])
}
}``````

2. Slice

In Go, slices are the same sequence of elements of variable length and fixed capacity. Go slicing is essentially an array. The capacity is fixed because the length of the array is fixed, and the capacity of the slice is the length of the hidden array. Variable length means variable in the range of array lengths.

(1) slice creation.

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), it can be seen that the difference between creating slices and creating array only 1 is whether there is a number in "[]" before Type. Because slices are of variable length. Here is 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}
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, 4 slices were created, 3 empty slices and 1 slice with value.

(2) slices with hidden array:

A slice is a reference to a hidden array, and the slice for that slice also references the same array. In the following example, 1 slice slice0 is created, and 2 slices slice1 and slice2 are created according to this 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)
}``````

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, slicing slice0, slice1, and slice2 are references to the same underlying array, so slice2 changes, and the other two change.

(3) traverse and modify 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~~~~~~ The index traversal ~~~~~~")
for index := range slice0 {
fmt.Print(slice0[index], " ")
}
fmt.Println("\n~~~~~~ Element indexes are used together ~~~~~~")
for index, ele := range slice0 {
fmt.Print(ele, slice0[index], " ")
}
fmt.Println("\n~~~~~~ Modify the ~~~~~~")
for index := range slice0 {
slice0[index] = "9"
}
fmt.Println(slice0)
}``````

As mentioned above, the first three loops use different for range loops. When for is followed by range and range is preceded by two elements, the first element represents the index and the second element represents the element value.

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

Only an index can modify an element. If, in the first traversal, ele is assigned to 7, the result has no effect. Because in the element traversal, ele is the value pass and ele is the copy of the slice element, modifying it will not affect the original value, while in the fourth traversal -- 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 indexes are used together ~~~~~~ ~
aa bb cc dd ee
~ ~ ~ ~ ~ ~ change ~ ~ ~ ~ ~ ~
[9 9 9 9 9]

(4), append, copy slices:

``````func test11() {
slice := []int32{}
fmt.Printf("slice Length is: %d,slice To: %v\n", len(slice), slice)
slice = append(slice, 12, 11, 10, 9)
fmt.Printf(" Additional, slice Length is: %d,slice To: %v\n", len(slice), slice)
slicecp := make([]int32, (len(slice)))
fmt.Printf("slicecp Length is: %d,slicecp To: %v\n", len(slicecp), slicecp)
copy(slicecp, slice)
fmt.Printf(" After copying the assignment, slicecp Length is: %d,slicecp To: %v\n", len(slicecp), slicecp)
}``````

Append and copy slices using the built-in functions append and copy. The copy function returns the number of last copied elements.

(5), built-in function append

The built-in function append can append one or more other values of the same type to a slice. If the number of appended elements exceeds the size of the original slice, a new slice in a new array is returned. If not, a whole new slice of the original array is returned. In any case, append had no effect on the original slice. Here's an example:

``````func test12() {
slice := []int32{1, 2, 3, 4, 5, 6}
slice2 := slice[:2]
_ = append(slice2, 50, 60, 70, 80, 90)
fmt.Printf("slice To: %v\n", slice)
fmt.Printf(" Operation of slices: %v\n", slice2)
_ = append(slice2, 50, 60)
fmt.Printf("slice To: %v\n", slice)
fmt.Printf(" Operation of slices: %v\n", slice2)
}``````

As shown above, the append method was used twice, and the result returned a completely different result, because the second time the append method appended no more elements than the slice capacity. However, slice2 in the original slice had no effect. Results:

slice is: [1, 2, 3, 4, 5, 6]
Section of operation: [1, 2]
slice is: [1, 2, 50, 60, 5, 6]
Section of operation: [1, 2]