Creating a slice with make
Slices can be created with the built-in make
function; this is how you create dynamically-sized arrays.
The make
function allocates a zeroed array and returns a slice that refers to that array:
1a := make([]int, 5) // len(a) = 5
To specify a capacity, pass a third argument to make
:
1b := make([]int, 0, 5) // len(b) = 0, cap(b)=5
2
3b = b[:cap(b)] // len(b)=5, cap(b)=5
4b = b[1:] // len(b)=4, cap(b)=4
References
#slice #structured #capacity #reference #length #zero #array #nil #make #data #golang #create #type