From 4e03318a5d536fb5ef10111972a179882640d799 Mon Sep 17 00:00:00 2001 From: GreatDiscovery <1303535630@qq.com> Date: Thu, 14 Nov 2024 22:27:11 +0800 Subject: [PATCH] add: the difference between len and cap of slice --- test/basic/collection/collection_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/basic/collection/collection_test.go b/test/basic/collection/collection_test.go index fa8a770..c066a67 100644 --- a/test/basic/collection/collection_test.go +++ b/test/basic/collection/collection_test.go @@ -109,4 +109,16 @@ func TestSliceCap(t *testing.T) { // 等于从索引 1 到底层数组末尾的元素数 assert.Equal(t, cap(newSlice), 4) assert.Equal(t, newSlice, []int{1, 2}) + + // 访问数组越界 + assert.PanicMatches(t, func() { + fmt.Println("newSlice[3]=", newSlice[3]) + }, "runtime error: index out of range [3] with length 2") + + // newSlice不能随意更改老的slice + newSlice = append(newSlice, 5) + assert.Equal(t, len(oldSlice), 5) + assert.Equal(t, oldSlice, []int{0, 1, 2, 5, 4}) + assert.Equal(t, oldSlice[3], 5) + assert.Equal(t, newSlice, []int{1, 2, 5}) }