instructions

Slice is a convenient data structure for manipulating arrays provided by GO. Arrays can automatically grow and shrink as needed, and slicing dynamically adds the underlying array data through the Append function. Because the underlying layer of slicing is an array, memory is allocated in contiguous chunks of memory, so slicing can get the same benefits of indexing, iteration, and garbage collection optimization as arrays.

Statement section

Make declaration section

Int The default value for initialization is 0
slice:=make([]int.4)
fmt.Println(slice)

/ / output
[0 0 0 0]
Copy the code

Slice literal

If you specify a value in the [] operator, you create an array instead of a slice. Slices are created only if no value is specified

// Create a slice of capacity 4 and assign an initial value
slice:=[]int{1.2.3.4}
fmt.Println(slice)

/ / output
[1 2 3 4]
Copy the code

nilslice

// Declaring nil slices does not initialize default values at this point
var slice []int
fmt.Println(slice)

/ / output
[]
Copy the code

Empty section

// Empty slices are not initialized with default values
slice:=[]int{}
fmt.Println(slice)

/ / output
[]
Copy the code

Both slices share one underlying data

Modifying data

  • Declares that two slices share an underlying array

    Slice2 := slice[1:3] slice2 := slice[x:y]

    So the length of slice2 is: y-x (3-1=2). Capacity: 4-x (4-1=3)

    slice := []int{1.2.3.4}
    slice2 := slice[1:3]
    fmt.Println(slice)
    fmt.Println(slice2)
    
    / / output
    [1 2 3 4]
    [2 3]
    Copy the code

  • Modify the data of slice2 slices

    Slice values have also been modified

    slice2[1] = 99
    fmt.Println(slice)
    fmt.Println(slice2)
    
    / / output
    [1 2 99 4]
    [2 99]
    Copy the code

Add Data operation

Add data to Slice2 based on the above data

Slice values are also modified because the underlying array is the same

slice2 = append(slice2,66)
fmt.Println(slice)
fmt.Println(slice2)

/ / output
[1 2 99 66]
[2 99 66]
Copy the code

### make the adding operation not affect the original slice

The solution to this problem is to use the third argument of the slice literal when assigning

**slice2 := slice[1:2:2] slice2 := slice[x:y:z]

So the length of slice2 is: y-x (2-1=1), and the capacity of slice2 is: z-x (2-1=1).

slice := []int{1.2.3.4}
//slice2 := slice[1:3]
// Set the capacity limit to 2-1=1
slice2 := slice[1: 2:2]
// Add the slice2 parameter to expand the capacity
slice2 = append(slice2,66)

fmt.Println(slice)
fmt.Println(slice2)

/ / output
[1 2 3 4]
[2 66]
Copy the code

capacity

When the slice capacity is less than 1000 elements, the capacity is always increased exponentially. Once the slice capacity exceeds 1000 elements, the capacity growth factor is set to 1.25, that is, the capacity is increased by 25% each time. As GO continues to be updated iteratively, this growth algorithm may change.

slice := []int{1.2.3.4}
fmt.Printf("Capacity before capacity expansion: %d\n".cap(slice))
fmt.Println(Content:",slice)
fmt.Println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -")
slice = append(slice,88)
fmt.Printf("Capacity after expansion: %d\n".cap(slice))
fmt.Println(Content:",slice)

/ / outputCapacity before capacity Expansion:4Content:1 2 3 4] ------------------------ Capacity after expansion:8Content:1 2 3 4 88]
Copy the code