This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Slice slice

This was the first time I was exposed to this concept, and I was wondering, what kind of container is this

A slice is a dynamic array

Slice is a reference type, so the slice variable is a pointer

Slices have concepts of length and capacity

Length is the number of elements it actually stores at the moment

Capacity is the maximum number of elements that the slice can store

So the length is never greater than the capacity

Slices are more common in Go than arrays

Defining a slice

You do not need to specify the length of the slice because it is dynamic

Format: var XXX []type

Define an empty slice

/* Output: []int(nil) S0 capacity: 0 S0 length: 0 */
var s0 []int
fmt.Printf("%#v\n", s0)
fmt.Println("Capacity of s0:".cap(s0))
fmt.Println("Length of s0:".len(s0))
Copy the code

Array to create slices

// Define a slice in the same way you define an array
// By definition, a slice is an array with no specified length
s1 := []int {1.3.5}
fmt.Printf("%#v\n", s1)
Copy the code

Use make to create slices

/* Create an int slice of size 5 and length 3 */
s2 := make([]int.3.5)
fmt.Printf("%#v\n", s2)
fmt.Println("Capacity of S2:".cap(s2))
fmt.Println("Length of S2:".len(s2))

/* Capacity is a reliable parameter, we can only pass length */
s3 := make([]int.3)
fmt.Println("S3 capacity:".cap(s3))
fmt.Println("Length of s3:".len(s3))
Copy the code

Generates a slice from an array

Slice := array[start:end: Max] Array is the target array. Start: indicates that the array starts at this subscript and includes the elements at this position. End: indicates that the array ends at this subscript and excludes the elements at this position

// Generate a slice from the array
arr := [5]int {1.2.3.4.5}
s4 := arr[1:3:5]
fmt.Printf("%#v\n", s4)
fmt.Println("Capacity of S4:".cap(s4))
fmt.Println("Length of S4:".len(s4))
Copy the code

Another slice is generated by slicing

/* Output s6 capacity: 10 S6 length: 6 */
s5 := make([]int.5.10)
s6 := s5[:6]
fmt.Printf("%#v\n", s5)
fmt.Println("S6 capacity:".cap(s6))
fmt.Println("S6 length:".len(s6))
Copy the code

Sections were reslicing

The process of obtaining a new slice by changing the length of the slice, called slice reorganization, will continue to reference the array of the original slices, causing memory overflow

s7 := getSlice()
fmt.Println(cap(s7), len(s7), &s7[0])

func getSlice(a) []byte {
	bytes := make([]byte.100)
	fmt.Println(cap(bytes), len(bytes), &bytes[0])
	return bytes[:3]}Copy the code

The copy function

The copy function generates a new slice that points to a new array */
s8 := getSlice2()
fmt.Println(cap(s8), len(s8), &s8[0])

func getSlice2(a) []byte {
	bytes := make([]byte.100)
	fmt.Println(cap(bytes), len(bytes), &bytes[0])
	res := make([]byte.3)

	copy(res, bytes[:3])
	return res
}
Copy the code

Append function

// Concatenate individual elements
s9 := []int {1.2.3}
s10 := append(s9, 4.5.6)
fmt.Println(s10)

// Splice some elements in the slice
s11 := []int {1.3.5}
s12 := []int {2.4.6}
s13 := append(s11[1:3], s12[:2]...). fmt.Println(s13)// Splice all the elements in the slice
s14 := append(s11, s12...)
fmt.Println(s14)
Copy the code

The array to which the different slices point

s15 := []int {1.2.3.4.5.6}
s16 := s15[:3]

// Now both slices point to the same array
fmt.Println("First slice:", s15, "Section address:", &s15[0])
fmt.Println("The second slice:", s16, "Section address:", &s16[0])

// Change the value of the second slice
s16[0] = 7
s16[1] = 8

// Both slices still point to the same array
fmt.Println("First slice:", s15, "Section address:", &s15[0])
fmt.Println("The second slice:", s16, "Section address:", &s16[0])

// The append function requests a new array when the size of the array is not sufficient for slicing
s16 = append(s16, 10.11.12.13)
fmt.Println("First slice:", s15, "Section address:", &s15[0])
fmt.Println("The second slice:", s16, "Section address:", &s16[0])
fmt.Println(s15, s16)
Copy the code

conclusion

Initial knowledge slice, at a loss. Deeply understand, doubly love. The slicing is really super flexible, super easy to use. There are some pitfalls to avoid, such as regrouping slices and using different slices on the same array. Others are very nice!!