WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Slices


Go language slices are abstractions of arrays.

The length of the Go array cannot be changed. Such a set is not suitable in a particular scenario. Go provides a flexible, powerful built-in type slice ("dynamic array"), the length of the slice compared to the array. It is not fixed, and elements can be added, which may increase the capacity of the slice when it is added.


Define a slice

You can declare an array of unspecified size to define a slice:

var identifier []type

Slices do not need to specify the length.

Or use the make() function to create a slice:

var slice1 []type = make ([]type, len)

Can also be abbreviated as 

Slice1 := make([]type, len)< /pre>

You can also specify a capacity, where capacity is an optional argument .

make([]T, length, capacity)

where len is the length of the array and is also the initial length of the slice.

Slice initialization

s :=[] int { 1,2,3 } 

Initialize the slice directly, [] indicates the slice type, and the initial values ​​of {1, 2, 3} are 1, 2, 3. The cap=len=3

s := arr[:] 

Initialize slice s, which is a reference to array arr

s := arr[startIndex: endIndex] 

Create an element in arr from subscript startIndex to endIndex-1 as a new slice

s := arr[startIndex:] 

The default endIndex will represent the last element up to arr

s := arr[:endIndex] 

The default startIndex will start with the first element of arr

s1 := s[startIndex: endIndex] 

Initialize slice s1 by slice s

s :=make([]int,len,cap) 

Initialize slice s with the built-in function make(), []int identifies the slice whose element type is int


len() and cap() functions

Slices are indexable and can be obtained by the len() method.

Slices provide a way to calculate capacity cap() can measure how long a slice can be.

The following are specific examples:

example

package main

import "fmt"

func main() {
   var numbers = make([]int,3,5)

   printSlice(numbers)
}

func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

The above example runs the output as:

len=3 cap=5 slice=[0 0 0]

empty (nil) slice

A slice defaults to nil before uninitialization, with a length of 0, Examples are as follows:

example

package main

import "fmt"

func main() {
   var numbers []int

   printSlice(numbers)

   if(numbers == nil){
      fmt.Printf("Slice is empty")
   }
}

func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

The above example runs the output as:

len=0 cap=0 slice=[]
Slice is empty

Slice interception

You can set the cut slice by setting the lower and upper limits [lower-bound:upper-bound],Examples are as follows:

example

package main

import "fmt"

func main() {
   /* Create a slice */
   numbers := []int{0,1,2,3,4,5,6,7,8}  
   printSlice(numbers)

   /* Print raw slices */
   fmt.Println("numbers ==", numbers)

   /* Print child slice from index 1 (inclusive) to index 4 (not included)*/
   fmt.Println("numbers[1:4] ==", numbers[1:4])

   /* The default lower limit is 0*/
   fmt.Println("numbers[:3] ==", numbers[:3])

   /* The default lower limit is len(s)*/
   fmt.Println("numbers[4:] ==", numbers[4:])

   numbers1 := make([]int,0,5)
   printSlice(numbers1)

   /* Print sub-slice from index  0(Including) to index 2 (not included) */
   number2 := numbers[:2]
   printSlice(number2)

   /* Print sub-slices from index 2 (inclusive) to index 5 (not included)*/
   number3 := numbers[2:5]
   printSlice(number3)

}

func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

Execute the above code output as:

len=9 cap=9 slice=[0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
numbers[1:4] == [1 2 3]
numbers[:3] == [0 1 2]
numbers[4:] == [4 5 6 7 8]
len=0 cap=5 slice=[]
len=2 cap=9 slice=[0 1]
len=3 cap=7 slice=[2 3 4]

append() and copy() functions

If you want to increase the size of the slice, we must create a new larger slice and copy the contents of the original slice.

The following code describes the copy method from a copy slice and the append method that appends a new element to the slice.

example

package main

import "fmt"

func main() {
   var numbers []int
   printSlice(numbers)

   /* Allow additional empty slices */
   numbers = append(numbers, 0)
   printSlice(numbers)

   /* Add an element to the slice */
   numbers = append(numbers, 1)
   printSlice(numbers)

   /* Add multiple elements at the same time */
   numbers = append(numbers, 2,3,4)
   printSlice(numbers)

   /* Create a slice numbers1 Is twice the capacity of the previous slice*/
   numbers1 := make([]int, len(numbers), (cap(numbers))*2)

   /* copy numbers Content to numbers1 */
   copy(numbers1,numbers)
   printSlice(numbers1)  
}

func printSlice(x []int){
   fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}

The above code execution output is:

len=0 cap=0 slice=[]
len=1 cap=1 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=6 slice=[0 1 2 3 4]
len=5 cap=12 slice=[0 1 2 3 4]