Latest web development tutorials

Go Language slice (Slice)

Go Language slice is an array of abstraction.

Go length of the array can not be changed, so the collection is not as useful in certain scenarios, Go provides a flexible, powerful built-in type section ( "dynamic array"), as compared to the array slice length is not fixed , the elements can be added when additional capacity increases may make slices.


Defined slice

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

var identifier []type

Sliced ​​length-explanatory.

Or to create a slice using make () function:

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

也可以简写为

slice1 := make([]type, len)

You can also specify the capacity, where capacity is optional.

make([]T, length, capacity)

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

Slice initialization

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

Direct initialization sections [] represent a slice type, {1,2,3} initialization values ​​followed by 1,2,3 its cap = len = 3

s := arr[:] 

Initialization slice s, is a reference to the array arr

s := arr[startIndex:endIndex] 

It will create a new slice to the element at index startIndex endIndex 1-under in arr

s := arr[startIndex:] 

The last element has to arr default when endIndex

s := arr[:endIndex] 

Indicating starting from the first element of arr time default startIndex

s1 := s[startIndex:endIndex] 

Slice by slice s initialize s1

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

Through built-in function make () Initializes slice s, [] int identifies its elements of type int slices


len () and cap () function

Slice is indexed and can be obtained by the length len () method.

Slice provides a method to calculate the capacity cap () can be measured up to a maximum number of slices.

The following specific examples:

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)
}

Run the above example output is:

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

Empty (nil) sliced

A slice before uninitialized default is nil, a length of 0, examples are as follows:

package main

import "fmt"

func main() {
   var numbers []int

   printSlice(numbers)

   if(numbers == nil){
      fmt.Printf("切片是空的")
   }
}

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

Run the above example output is:

len=0 cap=0 slice=[]
切片是空的

Slice interception

You can set the intercept slice [lower-bound: upper-bound ] by setting lower and upper, examples are as follows:

package main

import "fmt"

func main() {
   /* 创建切片 */
   numbers := []int{0,1,2,3,4,5,6,7,8}   
   printSlice(numbers)

   /* 打印原始切片 */
   fmt.Println("numbers ==", numbers)

   /* 打印子切片从索引1(包含) 到索引4(不包含)*/
   fmt.Println("numbers[1:4] ==", numbers[1:4])

   /* 默认下限为 0*/
   fmt.Println("numbers[:3] ==", numbers[:3])

   /* 默认上限为 len(s)*/
   fmt.Println("numbers[4:] ==", numbers[4:])

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

   /* 打印子切片从索引  0(包含) 到索引 2(不包含) */
   number2 := numbers[:2]
   printSlice(number2)

   /* 打印子切片从索引 2(包含) 到索引 5(不包含) */
   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 results:

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 () function

If you want to increase the capacity of the slice, we must create a new and bigger slice of the original fragments of the content is copied.

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

package main

import "fmt"

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

   /* 允许追加空切片 */
   numbers = append(numbers, 0)
   printSlice(numbers)

   /* 向切片添加一个元素 */
   numbers = append(numbers, 1)
   printSlice(numbers)

   /* 同时添加多个元素 */
   numbers = append(numbers, 2,3,4)
   printSlice(numbers)

   /* 创建切片 numbers1 是之前切片的两倍容量*/
   numbers1 := make([]int, len(numbers), (cap(numbers))*2)

   /* 拷贝 numbers 的内容到 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 output is the above code is executed:

len=0 cap=0 slice=[]
len=1 cap=2 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=8 slice=[0 1 2 3 4]
len=5 cap=16 slice=[0 1 2 3 4]