var arr = [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} var slice0 []int = arr[2:8] var slice1 []int = arr[0:6]//可以简写为 var slice []int = arr[:end] var slice2 []int = arr[5:10]//可以简写为 var slice[]int = arr[start:] var slice3 []int = arr[0:len(arr)]//var slice []int = arr[:] var slice4 = arr[:len(arr)-1]//去掉切片的最后一个元素
切片追加(append)
1 2 3 4 5 6 7 8 9 10
vara = []int{1, 2, 3} fmt.Printf("slice a : %v\n", a) varb = []int{4, 5, 6} fmt.Printf("slice b : %v\n", b) c := append(a, b...) fmt.Printf("slice c : %v\n", c) d := append(c, 7) fmt.Printf("slice d : %v\n", d) e := append(d, 8, 9, 10) fmt.Printf("slice e : %v\n", e)
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !