Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Spring Recruit punch card day 5 chapter 7.

Today we will continue to update the Go language learning record.

Slicing, unlike arrays, is a reference type, not a value type. Arrays are fixed in length, while slices are variable in length.

define

var s1 []int    // Define a slice that holds an element of type int
var s2 []string // Define a slice that holds elements of type string
fmt.Println(s1, s2)
fmt.Println(s1 == nil) //true: no memory space is opened
fmt.Println(s2 == nil) //true
Copy the code

Print result:

We declared the definition successful, but did not open up memory space, because s1, s2 are nil

Define and initialize

We can initialize it at the same time we define it

var s1 = []int{1.2.3}
var s2 = []string{"Beiyuan"."Changyang county"."Wangjing"}
fmt.Println(s1, s2)
fmt.Println(s1 == nil) //false
fmt.Println(s2 == nil) //false
Copy the code

Print result:

The values of s1 and s2 are not nil

Length and capacity

Len () and cap() were used to obtain the length and capacity of slices

fmt.Printf("len(s1):%d cap(s1):%d\n".len(s1), cap(s1))
fmt.Printf("len(s2):%d cap(s2):%d\n".len(s2), cap(s2))
Copy the code

Print result:

As we expected, both length and capacity are 3

Get the slice from the array

I already mentioned the relationship between arrays and slicing in the beginning, so here we go further:

  1. The essence of slicing is to operate on arrays, but arrays are of fixed length and slices are of variable length
  2. A slice is a reference type and can be understood as a fragment of a reference array; Arrays are value types. Assigning A value from array A to array B creates new memory space for array B. Changing the value of array B does not affect array A. Slices, as reference types, refer to the same memory address, which can affect each other.
// Define an array
a1 := [...]int{1.2.3.4.5.6.7.8.9}
s3 := a1[0:4] // Cut based on an array [0:4]
fmt.Println(s3)
Copy the code

Print result:

Note: a1[0:4] is cut based on an array [0:4] which is [1,2,3,4]

More examples of cutting methods

a1 := [...]int{1.2.3.4.5.6.7.8.9}
s4 := a1[2:4] / / [3, 4]
s5 := a1[:4] / / [1, 2, 3, 4]
s6 := a1[2:] //[3 4 5 6 7 8 9]
s7 := a1[:]  //[1 2 3 4 5 6 7 8 9]
fmt.Println(s4)
fmt.Println(s5)
fmt.Println(s6)
fmt.Println(s7)
Copy the code

Print result:

Analytic: comply with the above mentioned left contains, does not contain the right principle s4 from subscript 2 began to intercept, capture to subscript 4 s5 the first parameter is omitted, said from 0 subscript intercept s6 the second parameter is omitted, said intercepted the last element s7 two parameter is omitted, only fill in the middle of the colon, and said to take all the elements

Length and capacity of slice

The length of the slice is easily understood as the number of elements

The length from the first element of the slice to the last element in the underlying array referenced by the slice.

This may sound a little abstract, but it makes sense to look at the following chestnut:

a1 := [...]int{1.2.3.4.5.6.7.8.9}

s5 := a1[:4] / / [1, 2, 3, 4]
s6 := a1[2:] //[3 4 5 6 7 8 9]
s7 := a1[:]  //[1 2 3 4 5 6 7 8 9]

fmt.Printf("len(s5):%d cap(s5):%d\n".len(s5), cap(s5)) / / 4 and 9
fmt.Printf("len(s6):%d cap(s6):%d\n".len(s6), cap(s6)) / / July 7
fmt.Printf("len(s7):%d cap(s7):%d\n".len(s7), cap(s7)) / / 9 9
Copy the code

Print result:

A1 is an array of 9 length and 9 capacity, ranging from 1 to 9

S5 / S6 / S7 are all slices of array A1.

The length of S5 is 4, because there are only 4 elements: 1, 2, 3 and 4, and the capacity is 9. Because the first element of S5 slice is 1, and the last element of the underlying array A1 of S5 is 9, 1~9, so the capacity of S5 is 9.

S6 has length 7, because s6 has elements 3 and 9; S6 also has capacity 7, because the last element of s5’s underlying array is 9, 3, and 9, so it has capacity 7.

S7 is easier to understand. Both length and capacity are 9.

Slice and slice

We can re-slice slices

For example, we sliced the above data again to test it

s8 :=s6[3:]
fmt.Printf("len(s8):%d cap(s8):%d\n".len(s8), cap(s8)) / / 4 4
Copy the code

Print result:

We know that the slices can be sliced again. As for the length and container, this output is expected.

Slice is a reference type

Let’s prove that a slice is a reference type

// Define an array
a1 := [...]int{1.2.3.4.5.6.7.8.9}
// There are arrays cut into slices s6
s6 := a1[2:] //[3 4 5 6 7 8 9]
// slice slice again and assign to s8
s8 :=s6[3:] / / [6, 7, 8 and 9]
// Modify the original array with the subscript 2 value from 3 to 333
a1[2] = 333
// Print s6 and find that 3 in S6 also becomes 333
fmt.Println("s6:", s6) //[333 4 5 6 7 8 9]
// Since s8 is based on s6 slicing, let's test slicing and slicing
fmt.Println("s8:", s8) / / [6, 7, 8 and 9]
// We change the index of the original array from 6 to 666
a1[5] = Awesome!
// Print s8 slices and get the result that 6 is also 666
fmt.Println("s8:", s8) / / [666 7 8 9]
Copy the code

Print result:

Slice is a reference type. When the underlying array changes, whether slice or slice, the value changes. Because they’re using a block of memory, a reference to a memory address.

conclusion

This article introduces the characteristics of slices, how to define slices, if sliced by an array, and the characteristics of the reference type of slices.

The last

Thanks for reading and welcome to like, favorites,coin(attention)!!