一、数组定义、遍历、截取、比较
package array_test import "testing" //数组 func TestArrayInit(t *testing.T) { //默认初始化零值 var arr [3]int //初始化并赋值 arr1 := [4]int{1, 2, 3, 4} //初始化,自动获取数组长度 arr2 := [...]int{1, 2, 3, 4} t.Log(arr[0], arr[1]) t.Log(arr1, arr2) // 数组遍历 for i := 0; i < len(arr2); i++ { t.Log(arr2[i]) } //range遍历 for i, e := range arr2 { t.Log(i, e) } // range遍历 使用占位 for i, _ := range arr2 { t.Log(i) } // 多维数组申明 arr3 := [2][2]int{{1, 2}, {1, 2}} t.Log(arr3) // 数组截取[开始(包含),结束(不包含)] 使用len不会越界 arr4 := [...]int{1, 2, 3, 4, 5} t.Log(arr4[1:2]) //2 t.Log(arr4[1:]) //2 3 4 5 t.Log(arr4[:2]) //1 2 } // 数组可比较,维数、顺序、值相同 true func TestArrayComparing(t *testing.T) { a := [...]int{1, 2, 3, 4} b := [...]int{1, 2, 3, 4} t.Log(a == b) }
=== RUN TestArrayInit array_test.go:13: 0 0 array_test.go:14: [1 2 3 4] [1 2 3 4] array_test.go:17: 1 array_test.go:17: 2 array_test.go:17: 3 array_test.go:17: 4 array_test.go:21: 0 1 array_test.go:21: 1 2 array_test.go:21: 2 3 array_test.go:21: 3 4 array_test.go:25: 0 array_test.go:25: 1 array_test.go:25: 2 array_test.go:25: 3 array_test.go:29: [[1 2] [1 2]] array_test.go:32: [2] array_test.go:33: [2 3 4 5] array_test.go:34: [1 2] --- PASS: TestArrayInit (0.00s) === RUN TestArrayComparing array_test.go:41: true --- PASS: TestArrayComparing (0.00s) PASS
二、切片定义、添加、扩容、比较
package slice_test import "testing" //切片 func TestSlice(t *testing.T) { // 申明 var s0 []int t.Log(len(s0), cap(s0)) s0 = append(s0, 50) t.Log(len(s0), cap(s0)) //申明并初始化 s1 := []int{1, 2, 3, 4} t.Log(len(s1), cap(s1)) //使用make初始化 s2 := make([]int, 3, 5) t.Log("初始化", len(s2), cap(s2)) s2 = append(s2, 6) t.Log(s2[0], s2[1], s2[2], s2[3]) t.Log("添加元素后:", len(s2), cap(s2)) } //切片扩容,翻倍 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) { //初始化 year := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "sep", "Oct", "Nov", "Dec"} t.Log("year", len(year), cap(year)) //切片1 q2 := year[3:6] t.Log(q2, len(q2), cap(q2)) //切片2 summer := year[5:8] t.Log(summer, len(summer), cap(summer)) summer[0] = "Unknow" t.Log(q2) t.Log(year) } //切片比较 func TestSliceComparing(t *testing.T) { a := []int{1, 2, 3, 4} b := []int{1, 2, 3, 4} t.Log(a, b) //t.Log(a == b) }
=== RUN TestSlice slice_test.go:9: 0 0 slice_test.go:11: 1 1 slice_test.go:14: 4 4 slice_test.go:17: 初始化 3 5 slice_test.go:19: 0 0 0 6 slice_test.go:20: 添加元素后: 4 5 --- PASS: TestSlice (0.00s) === RUN TestSliceGrowing slice_test.go:29: 1 1 slice_test.go:29: 2 2 slice_test.go:29: 3 4 slice_test.go:29: 4 4 slice_test.go:29: 5 8 slice_test.go:29: 6 8 slice_test.go:29: 7 8 slice_test.go:29: 8 8 slice_test.go:29: 9 16 slice_test.go:29: 10 16 --- PASS: TestSliceGrowing (0.00s) === RUN TestSliceShareMemory slice_test.go:38: year 12 12 slice_test.go:41: [Apr May Jun] 3 9 slice_test.go:44: [Jun Jul Aug] 3 7 slice_test.go:46: [Apr May Unknow] slice_test.go:47: [Jan Feb Mar Apr May Unknow Jul Aug sep Oct Nov Dec] --- PASS: TestSliceShareMemory (0.00s) === RUN TestSliceComparing slice_test.go:54: [1 2 3 4] [1 2 3 4] --- PASS: TestSliceComparing (0.00s) PASS
三、区别
1、数组是定长度,切片是边长度;
2、数组可以相互比较,切片不可以;
3、数组是值传递,切片是引用传递;