Slice of GO Foundation

Keywords: Go

What is slicing

Go language slicing is an abstraction of arrays.

  • The length of go array cannot be changed, so such a set is not suitable in a specific scene. Go provides a flexible and powerful built-in type slice ("dynamic array");
  • Compared with array, the length of slice is not fixed. You can append elements, which may increase the capacity of slice.
  • Slices themselves have no data, they are just references to existing arrays.
  • Compared with array, slice does not need to set the length, and does not need to set the value in [], so it is relatively free
  • Conceptually speaking, slice is like a structure, which contains three elements:

Structure of slice:

  • Pointer to the start position specified by slice in the array
  • Length, length of SPslice
  • The maximum length, that is, the length from the beginning of slice to the last position of the array

II. Use of slices

Slice declaration

 

    s1 := make([]int, 5)
    s2 := make([]int, 5, 7)

 

Initialization of slices:

 

 

nums := []int{1, 2, 3, 4, 5}

len() and cap() functions

1. The length of slice is the number of elements in slice.

2. The capacity of slice is the number of elements in the underlying array starting from the index of slice creation.

3. Slicing is indexable, and length can be obtained by Len () method. Slicing provides a method of calculating capacity cap (), which can measure the maximum length of slicing. [the result of cap() calculated by array is the same as len()]

4. Slice is actually to get a part of the array, len slice < = cap slice < = len array

package main

import "fmt"

func main() {
    s1 := make([]int, 5)
    s2 := make([]int, 5, 7)
    printSlice(s1) //length:5,cap=5,slice=[0 0 0 0 0]
    printSlice(s2) //length:5,cap=7,slice=[0 0 0 0 0]
    //Create slicing
    nums := []int{1, 2, 3, 4, 5}
    printSlice(nums)
    //To cut a small slice from a slice
    nums1 := nums[1:4]
    printSlice(nums1) //length:3,cap=4,slice=[2 3 4] 
    nums2 := nums[:3]
    printSlice(nums2) //length:3,cap=5,slice=[1 2 3]
    nums3 := nums[2:]
    printSlice(nums3) //length:3,cap=3,slice=[3 4 5]
}
func printSlice(slc []int) {
    fmt.Printf("length:%d,cap=%d,slice=%v \n", len(slc), cap(slc), slc)
}

 

Common functions of slicing

(V) append() and copy() functions

1. Function append():

  • • add new elements to slice
  • • you can add one or more elements to slice or a slice.
  • The append function will change the contents of the array referenced by slice, thus affecting other slices that reference the same array.
  • • when append is used to append elements to slices, if the capacity is not enough (that is, (Cap len) = = 0), Go will create a new memory address to store the elements (which is inefficient).

2. Function copy: * copy slice element

  • • copy the elements in the source slice to the target slice, and return the number of copied elements
  • The copy method does not establish the connection between the source slice and the target slice. In other words, there is no connection between two slices, and one modification does not affect the other.
package main

import "fmt"

func main() {
    //Create slicing
    nums := []int{1, 2, 3, 4, 5}
    printSlice("nums", nums) //slice=nums,p=0xc00000c5d0,length:5,cap=5,slice=[1 2 3 4 5]
    //append Additive element
    nums = append(nums, 10)
    printSlice("nums", nums) //slice=nums,p=0xc000014230,length:6,cap=10,slice=[1 2 3 4 5 10]
    a := []int{6, 7, 8, 9}
    //Appended array
    nums = append(nums, a...)
    printSlice("nums", nums) //slice=nums,p=0xc000014230,length:10,cap=10,slice=[1 2 3 4 5 10 6 7 8 9]
    //Delete first element
    nums = nums[1:]
    printSlice("nums", nums) //slice=nums,p=0xc000014238,length:9,cap=9,slice=[2 3 4 5 10 6 7 8 9]
    //Delete last element
    nums = nums[:len(nums)-1]
    printSlice("nums", nums) //slice=nums,p=0xc000014238,length:8,cap=9,slice=[2 3 4 5 10 6 7 8]
    //Delete middle element
    b := int(len(nums) / 2)
    nums = append(nums[:b], nums[b+1:]...)
    printSlice("nums", nums) //slice=nums,p=0xc000014238,length:7,cap=9,slice=[2 3 4 5 6 7 8]

    //Section copy
    nums2 := make([]int, len(nums), cap(nums)*2)
    //copy Slice of is not associated
    copy(nums2, nums)
    printSlice("nums2", nums2) //slice=nums,p=0xc000014238,length:7,cap=9,slice=[2 3 4 5 6 7 8]

}
func printSlice(name string, slc []int) {
    fmt.Printf("slice=%v,p=%p,length:%d,cap=%d,slice=%v \n", name, slc, len(slc), cap(slc), slc)
}

3. Create slice with make

package main

import "fmt"
import "strconv"

func main() {
    str := make([]string, 0, 16)
    printSlice("str", str)
    for i := 0; i < 5; i++ {
        str = append(str, strconv.Itoa(i))
    }
    printSlice("str", str)

}
func printSlice(name string, slc []string) {
    fmt.Printf("slice=%v,p=%p,length:%d,cap=%d,slice=%v \n", name, slc, len(slc), cap(slc), slc)
}

Posted by Lodar on Sat, 02 Nov 2019 14:29:05 -0700