This article will introduce you to the variable-length "array" in golang - slice. I hope it will be helpful to you!
golang slice (slice)
(1) Definition of slice
A slice is a variable-length sequence of elements of the same type. It is a layer of encapsulation based on the array type. It is very flexible and supports automatic expansion.
Slice is a reference type, and its internal structure contains address, length and capacity. Slices are generally used to quickly operate on a collection of data. [Related recommendations: Go Video Tutorial]
The only difference between creating a slice and creating an array is whether there is a number in the "[]" before Type. If it is empty, it means a slice, otherwise represents an array. Because slices have variable length
var a []string //声明一个字符串切片 var b = []int{1,2,3} //声明一个整数类型切片并初始化 var c = []bool{false,true} //声明一个bool类型并且初始化
In fact, in actual situations, it is often understood as: slices are part of the array
num:=[3]int{0,1,2} //:前后表示数组内部的索引 sc1:=num[:] //获取数组的全部 sc2:=num[0:2] //左闭右开,取到的是[0,1] sc3:=num[1:] //取到的是[2] sc4:=num[:1] //取到的是[0]
(2) Use the make function to create slices
Here begins to correspond to the previous statement. The capacity of the slice is variable, which can be achieved using the make function. The essence of the slice is to encapsulate the underlying array, which contains three Information: pointer to the underlying array, length of the slice (len), and capacity of the slice (cap).
// make([]T, size, cap) sc=make([]int,2,10) fmt.Println(sc) // [0 0] fmt.Println(len(sc)) // len表示切片存储元素的长度 fmt.Println(cap(sc)) // cap表示最大可以存储的容量
(3) Traversal of slices
sc:=[]int{1,2,3} //普通for循环遍历 for i:=0;i<len(sc);i++{ fmt.Println(s[i]) } //for range遍历 for index,value:=range s{ fmt.Println(index,value) }
(4) append() method
Go language The built-in function append() can dynamically add elements to a slice. You can add one element at a time, multiple elements, or elements from another slice (followed by...).
var s1 []int //定义切片s1 s1 = append(s1,1) //在切片s1后面添加一个1的元素 s1 = append(s1,2,3,4) //在切片s1后面继续添加2,3,4 s2:=[]int{5,6,7} //定义切片s2 s1 = append(s1,s2) //把切片s2中的元素追加到切片s1中
(5)copy method
The built-in copy() function of Go language can quickly copy the data of one slice to another slice space. copy The usage format of the () function is as follows:
a:=[]int{1,2,3,4,5} b:=make([]int,5,5) copy(b,a) //把a切片中的元素copy到b切片中的元素 fmt.Println(b) //[1,2,3,4,5] b[0]=1000 fmt.Println(b) // [1000,2,3,4,5]
There is a problem here that needs attention. There is such a problem in actual development
a := []int{1, 2, 3, 4, 5} b := a fmt.Println(a) //[1 2 3 4 5] fmt.Println(b) //[1 2 3 4 5] b[0] = 1000 fmt.Println(a) //[1000 2 3 4 5] fmt.Println(b) //[1000 2 3 4 5] /* 由于切片是引用类型,所以a和b其实都指向了同一块内存地址。 修改b的同时a的值也会发生变化。 */
(6) Cleverly delete the Element
There is no special method to delete slice elements in the Go language. We can use the characteristics of the slice itself to delete elements. The code is as follows:
// 从切片中删除元素 a := []int{30, 31, 32, 33, 34, 35, 36, 37} // 要删除索引为2的元素 a = append(a[:2], a[3:]...) fmt.Println(a) //[30 31 33 34 35 36 37] /* 总结一下就是:要从切片a中删除索引为index的元素, 操作方法是a = append(a[:index], a[index+1:]...) */
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of Let's talk about slices in the Go language. For more information, please follow other related articles on the PHP Chinese website!