An array of

There are three common ways to initialize an array:

var a[4]int

b := [4]int{2.4}

c := [...]int{2.4}

Copy the code

The Go array is a value type. Assignments and passes copy the entire array data. To avoid data copying, use array Pointers:

func test(x *[2]int) {

    x[1] + =1

}

func main(a) {

    a := [2]int{2.3}

    test(&a)

}

Copy the code

Finally, we need to distinguish between pointer arrays and array Pointers:

func main(a) {

    x,y := 1.2

a := [...] *int{&x, &y} // The element is an array of Pointers

    p := &a // Store a pointer to an array address

}

Copy the code

The array of Go, in fact, we do not use much, generally we use slice, so for the array, master the above knowledge is ok, other knowledge about the array, when you need to use, refer to the relevant information.

slice

Create a slice

Slice belongs to the reference type, and there are two initialization modes, respectively:

s1 := make([]int.4.6)

s2 := []int{0.20.30.40.50.60}

Copy the code

S1 := make([]int, 4, 6) s1 := make([]int, 4, 6) s1 := make([]int, 4, 6) s1 := make([]int, 4, 6) s1 := make([]int, 4, 6)


S2 := [] int{10,20,30,40,50,60}


The section operator S [I :j:k], j and K are open intervals, as shown in the following figure:


Nil and empty slices

Nil slice pointer to nil denotes a non-existent slice:

var slice []int

Copy the code

An empty slice is usually used to represent an empty set. For example, if a database query fails to find a single result, an empty slice can be returned.

silce := make([]int , 0)  

slice := []int{}

Copy the code

One caveat: Calling the built-in append, Len, and Cap functions has the same effect whether you use nil slices or empty slices. And then slicing can only be evaluated with nil, so slicing is not supported.

Section capacity

Let’s start with a picture:


Go slice expansion policy: If the slice capacity is smaller by 1024 elements, the slice capacity is doubled during capacity expansion. This is also demonstrated in the example above, where the total capacity has doubled from 4 to 8. Once the number of elements exceeds 1024, the growth factor becomes 1.25, or 1/4 of the original capacity.

Let’s look at a case where a new array is not created during capacity expansion, which can cause problems:

func main(a) {  

   array := [4]int{10.20.30.40}

   slice := array[0:2]  / / 10 20

   newSlice := append(slice, 50/ / 10 20 to 50

   newSlice[1] + =10 / / 10 30 to 50

   // Slice =[10 30] array=[10 30 50 40]

}

Copy the code

Slice, newSlice, and array share the same array. When you modify newSlice[1], the underlying data is modified, and other data are also modified, which is very easy to cause inexplicable bugs!


Slice traversal

If you use range to traverse a Slice, the Value you get is actually a copy of the Value in the Slice. Each time you print the address of the Value, it will not change the Value of the Slice.


Welcome everyone to like a lot, more articles, please pay attention to the wechat public number “Lou Zai advanced road”, point attention, do not get lost ~~