The article suggests that

This paper summarizes the particularity of slice in GO language and points for attention when using slice.

Personal understanding, deficiencies welcome to point out.

Slice: a common data structure in go. It is based on arrays and represents collections of the same data type.

An array of

The array type in Go represents a collection of fixed-length data of the same type. The data is stored consecutively in memory and can be indexed by subscript, but there are special points:

  • Arrays are value types, and an array variable represents the entire array, not a pointer to the first element of the array, as in C.
  • Assigning an array to another array, or passing an array as a function argument, copies the entire array’s data instead of passing a pointer.
  • Array types include lengths, that is, [5]int and [10] are not one type.

Therefore, it is very inefficient to pass data through arrays in Go language, and slicing is usually used.

slice

A slice is a description of an array fragment, containing Pointers to the array fragment, len of the fragment’s length, and cap of the capacity (the maximum length of the array fragment), but the slice itself is not a true pointer type.

Characteristics of slices

  1. Append () is used to append data to the slice. The data is added to the end of the segment pointed to by the slice. When the length is equal to the capacity, the slice will be automatically expanded.
  2. When assigning values between slices or passing slices as function parameters, Pointers to array fragments are passed, so changing one affects the other.

The trap of slicing

When a slice is used as function parameter passing or shallow copy, the reason why changing the data of one slice will affect the other slice is that the two slices contain Pointers to the same array fragment.

Everything seems normal? However, when the expansion of a slice occurs, the data in the current slice will be copied to another memory area, and the address of the array segment of the slice will be changed. Therefore, when the data of one slice is modified during the expansion of the slice, the other slice will not be affected! This can only be solved by passing the address of the slice itself.

The code for capacity expansion error is as follows:

package main

import "fmt"

func testSlice(slice []int) {
slice = append(slice, 6, 7, 8, 9, 10)
fmt.Println("testSlice:",slice)
}
func main() {
slice := []int{1, 2, 3, 4, 5}

	testSlice(slice)
fmt.Println("main:",slice)
}
Copy the code

The nature of slices

So, a slice is not a pointer type. A slice data type is a compound data structure that contains a pointer to an array fragment, the length of the current array fragment, and the maximum capacity of the current array.

For an in-depth look at the underlying implementation of the Slice data type in Go, learn about Go on Github