创建
arr1 := new([len]type)
slice1 := make([]type, len)
初始化
arr1 := [...]type{i1, i2, i3, i4, i5}
arrKeyValue := [len]type{i1: val1, i2: val2}
var slice1 []type = arr1[start:end]
常用操作
-
将切片
b
的元素追加到a
之后a = append(a, b...)
-
复制切片
a
的元素到新的切片b
上b := make([]T, len(a)) copy(b, a)
-
删除位于索引
i
的元素a = append(a[:i], a[i+1:]...)
-
切除切片
a
中从索引i
至j
位置的元素a = append(a[:i], a[j:]...)
-
为切片
a
扩展j
个元素长度a = append(a, make([]T, j)...)
-
在索引
i
的位置插入元素x
a = append(a[:i], append([]T{x}, a[i:]...)...)
-
在索引
i
的位置插入长度为j
的新切片a = append(a[:i], append(make([]T, j), a[i:]...)...)
-
在索引
i
的位置插入切片b
的所有元素a = append(a[:i], append(b, a[i:]...)...)
-
取出位于切片
a
最末尾的元素x
x, a = a[len(a)-1], a[:len(a)-1]
-
将元素
x
追加到切片a
a = append(a, x)