func TestSliceInit(t *testing.T) {var s0 []int
t.Log(len(s0), cap(s0))
s0 = append(s0, 1)
t.Log(len(s0), cap(s0))
s1 := []int{1, 2, 3, 4}
t.Log(len(s1), cap(s1))
s2 := make([]int, 3, 5)
// 长度 3,容量 5
t.Log(len(s2), cap(s2))
// 只能拜访前三个元素,不能拜访 s2[3]
t.Log(s2[0], s2[1], s2[2])
s2 = append(s2, 1)
// 长度 4,容量 5
t.Log(len(s2), cap(s2))
// 能够拜访前四个元素,不能拜访 s2[4]
t.Log(s2[0], s2[1], s2[2], s2[3])
}
func TestSliceGrowing(t *testing.T) {s := []int{}
for i := 0; i < 10; i++ {s = append(s, i)
// 容量会翻倍增长
t.Log(len(s), cap(s))
}
}
func TestSliceShareMemory(t *testing.T) {months := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}
Q2 := months[3:6]
//[...] 3 9
t.Log(Q2, len(Q2), cap(Q2))
summer := year[5:8]
t.Log(summer, len(summer), cap(summer))
summer[0] = "Unknow"
t.Log(Q2)
t.Log(months)
}
func TestSliceComparing(t *testing.T) {a := []int{1, 2, 3, 4}
b := []int{1, 2, 3, 4}
// 切片不能进行比拟
if a == b {t.Log("equal")
}
}