Blogs
Go Slices: usage and internals
Arrays, slices (and strings): The mechanics of 'append'
Details
make
Look up in $GOROOT/src/runtime/slice.go:makeslice
.
append
https://stackoverflow.com/a/33405824/13133551
slicing
The length is the number of elements referred to by the slice. The capacity is the number of elements in the underlying array (beginning at the element referred to by the slice pointer).
copy
Look up in $GOROOT/src/runtime/slice.go:slicecopy
.
The copy function supports copying between slices of different lengths (it will copy only up to the smaller number of elements). In addition, copy can handle source and destination slices that share the same underlying array, handling overlapping slices correctly.
Codes
1 | package main |
Result: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20[2020-03-27 16:04:59] INFO in sliceCopy
[2020-03-27 16:04:59] INFO c: []
[2020-03-27 16:04:59] INFO c: [0 1 2 3 4]
[2020-03-27 16:04:59] INFO c: [0 1 2 3 4 5 6 7 8 9 0 0 0 0 0]
[2020-03-27 16:04:59] INFO c: [0 1 2 3 4]
[2020-03-27 16:04:59] INFO c: [256 1 2 3 4], s: [0 1 2 3 4 5 6 7 8 9]
[2020-03-27 16:04:59] INFO in sliceAppend
[2020-03-27 16:04:59] INFO after appending: 0, len(s): 1, cap(s): 1
[2020-03-27 16:04:59] INFO after appending: 1, len(s): 2, cap(s): 2
[2020-03-27 16:04:59] INFO after appending: 2, len(s): 3, cap(s): 4
[2020-03-27 16:04:59] INFO after appending: 3, len(s): 4, cap(s): 4
[2020-03-27 16:04:59] INFO after appending: 4, len(s): 5, cap(s): 8
[2020-03-27 16:04:59] INFO after appending: 5, len(s): 6, cap(s): 8
[2020-03-27 16:04:59] INFO after appending: 6, len(s): 7, cap(s): 8
[2020-03-27 16:04:59] INFO after appending: 7, len(s): 8, cap(s): 8
[2020-03-27 16:04:59] INFO after appending: 8, len(s): 9, cap(s): 16
[2020-03-27 16:04:59] INFO after appending: 9, len(s): 10, cap(s): 16
[2020-03-27 16:04:59] INFO in resultOfAppend
[2020-03-27 16:04:59] INFO in slicing