package main

 import "fmt"

 //切片的操作

 func main() {

     //创建slice
var s []int //zero value for slice is nil for i := ; i < ; i++ {
s = append(s, * i + )
}
fmt.Println(s) //[1 3 5 7 9 11 13 15 17 19] s1 := []int{, , , }
fmt.Println(s1) //[2 4 6 8]
fmt.Printf("cap:%d\n", cap(s1)) //cap:4 s2 := make( []int, )
fmt.Println(s2) //[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
fmt.Printf("cap:%d\n", cap(s2)) //cap:16 s3 := make( []int, , ) //32设置的是cap值
fmt.Println(s3) //[0 0 0 0 0 0 0 0 0 0]
fmt.Printf("cap:%d\n", cap(s3)) //cap:32 //复制slice
copy(s2, s1)
fmt.Println(s2,len(s2), cap(s2)) //[2 4 6 8 0 0 0 0 0 0 0 0 0 0 0 0] 16 16 //删除slice中的元素
s2 = append( s2[:], s2[:]...)
fmt.Println(s2, len(s2), cap(s2)) //[2 4 6 0 0 0 0 0 0 0 0 0 0 0 0] 15 16 front := s2[]
s2 = s2[:]
fmt.Println(front) //
fmt.Println(s2, len(s2), cap(s2)) //[4 6 0 0 0 0 0 0 0 0 0 0 0 0] 14 15 tail := s2[len(s2) - ]
s2 = s2[: len(s2) - ]
fmt.Println(tail) //
fmt.Println(s2, len(s2), cap(s2)) //[4 6 0 0 0 0 0 0 0 0 0 0 0] 13 15 }
05-28 18:59