package main
import "fmt"
func test1() {
fmt.Println("test1 ------------------")
s := []int{2, 3, 5, 7, 11, 13}
printSlice("s", s)
printSlice("s[1:4]", s[1:4])
printSlice("s[:3]", s[:3])
printSlice("s[4:]", s[4:])
a := s[2:5]
printSlice("a", a)
a[0] = 100
printSlice("a", a)
printSlice("s", s)
}
func test2() {
fmt.Println("test2 ------------------")
var arr [10]int
for i := 0; i < 10; i++ {
arr[i] = i
}
arrS := arr[:]
fmt.Println(arr)
printSlice("arrS", arrS)
arrS[0] = 100
fmt.Println(arr)
printSlice("arrS", arrS)
}
func test3() {
fmt.Println("test3 ------------------")
a := make([]int, 5)
printSlice("a", a)
//b := make([]int, 0, 5)
b := make([]int, 5, 5) //change len to 5, you will know why.
printSlice("b", b)
c := b[:2]
printSlice("c", c)
d := c[2:5]
printSlice("d", d)
c[0] = 200
fmt.Println("-------------")
printSlice("a", a)
printSlice("b", b)
printSlice("c", c)
printSlice("d", d)
d[0] = 100
fmt.Println("-------------")
printSlice("a", a)
printSlice("b", b)
printSlice("c", c)
printSlice("d", d)
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
func main() {
test1()
test2()
test3()
}