In the Go language, the append() function is used to dynamically add elements to a slice. You can add elements to the end of the slice and return the result; when calling the append function, you must use the original slice variable to receive the return value and append an element. You can use the "slice = append(slice,elem1,elem2)" statement. To append a slice, you can use the "slice = append(slice,anotherSlice...)" statement.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language’s built-in function append() can dynamically add elements to a slice.
slice = append(slice,elem1,elem2)
package main import "fmt" //切片进阶操作 func main(){ //append()为切片追加元素 s1 := []string {"火鸡面","辛拉面","汤达人"} fmt.Printf("s1=%v len(s1)=%d cap(s1)=%d\n",s1,len(s1),cap(s1)) //调用append函数必须用原来的切片变量接收返回值 s1 = append(s1,"小当家") //append追加元素,原来的底层数组装不下的时候,Go就会创建新的底层数组来保存这个切片 fmt.Printf("s1=%v len(s1)=%d cap(s1)=%d\n",s1,len(s1),cap(s1))//cap增加两倍 }
s1=[火鸡面 辛拉面 汤达人] len(s1)=3 cap(s1)=3 s1=[火鸡面 辛拉面 汤达人 小当家] len(s1)=4 cap(s1)=6
slice = append(slice,anotherSlice...)
package main import "fmt" //切片进阶操作 func main(){ //append()为切片追加元素 s1 := []string {"火鸡面","辛拉面","汤达人"} fmt.Printf("s1=%v len(s1)=%d cap(s1)=%d\n",s1,len(s1),cap(s1)) //调用append函数必须用原来的切片变量接收返回值 s1 = append(s1,"小当家") //append动态追加元素,原来的底层数组容纳不下足够多的元素时,切片就会开始扩容,Go底层数组就会把底层数组换一个 fmt.Printf("s1=%v len(s1)=%d cap(s1)=%d\n",s1,len(s1),cap(s1)) //调用append添加一个切片 s2 := []string{"脆司令","圣斗士"} s1 = append(s1,s2...)//...表示拆开切片,再添加 fmt.Printf("s1=%v len(s1)=%d cap(s1)=%d",s1,len(s1),cap(s1)) }
s1=[火鸡面 辛拉面 汤达人] len(s1)=3 cap(s1)=3 s1=[火鸡面 辛拉面 汤达人 小当家] len(s1)=4 cap(s1)=6 s1=[火鸡面 辛拉面 汤达人 小当家 脆司令 圣斗士] len(s1)=6 cap(s1)=6
package main import "fmt" func main(){ var a = make([]int, 5, 10) fmt.Println(a) fmt.Printf("%p\n",a) for i := 0; i <10; i++ { a = append(a,i) //%p 打印切片地址 fmt.Printf("%v,%p,cap(a):%d\n",a,a,cap(a)) } }
[0 0 0 0 0] 0xc0000180a0 [0 0 0 0 0 0],0xc0000180a0,cap(a):10 [0 0 0 0 0 0 1],0xc0000180a0,cap(a):10 [0 0 0 0 0 0 1 2],0xc0000180a0,cap(a):10 [0 0 0 0 0 0 1 2 3],0xc0000180a0,cap(a):10 [0 0 0 0 0 0 1 2 3 4],0xc0000180a0,cap(a):10 [0 0 0 0 0 0 1 2 3 4 5],0xc00007c000,cap(a):20 [0 0 0 0 0 0 1 2 3 4 5 6],0xc00007c000,cap(a):20 [0 0 0 0 0 0 1 2 3 4 5 6 7],0xc00007c000,cap(a):20 [0 0 0 0 0 0 1 2 3 4 5 6 7 8],0xc00007c000,cap(a):20 [0 0 0 0 0 0 1 2 3 4 5 6 7 8 9],0xc00007c000,cap(a):20
:
If you want to delete an element in slice s, the index of the deleted element is index, then the deletion process is
s = append ( s[ :index ], s[ index+1: ] )
package main import "fmt" func main(){ a1 := [...]int{1,2,5,3,78,9,4,9,23,32} s1 := a1[:] //得到切片 fmt.Println(s1) //删除索引为4的78 s1 = append(s1[:4],s1[5:]...) fmt.Println(s1) fmt.Println(a1) }
[1 2 5 3 78 9 4 9 23 32] [1 2 5 3 9 4 9 23 32] [1 2 5 3 9 4 9 23 32 32]
package main import "fmt" func main(){ a1 := [...]int{1,2,5,3,78,9,4,9,23,32} s1 := a1[:] //得到切片 fmt.Println(s1) //删掉索引为2和3的5,3 s1 = append(s1[:2],s1[4:]...) fmt.Println(s1) fmt.Println(a1) }
[1 2 5 3 78 9 4 9 23 32] [1 2 78 9 4 9 23 32] [1 2 78 9 4 9 23 32 23 32]
Go video tutorial, Programming teaching】
The above is the detailed content of How to use append() in go language. For more information, please follow other related articles on the PHP Chinese website!