Go Slices Are Dynamic Arrays
Jan 11, 2021
Despite what’s said about Go slices in this blog post, if you really know what dynamic arrays are internally, you should immediately recognize the resemblance between Go’s slices and a dynamic array.
To be more precise, a typical dynamic array implementation is like
typedef struct {
int *array;
int used;
int size;
} Array;
Where the underlying array is allocated by malloc
and will be reallocted by realloc
if need be. Now, let’s take a look at the internals of a Go slice. Oh, it has three parts too. A pointer to an underlying array, a length, and a capacity. Sounds familiar, right? How should we call such a structure if not “dynamic arrays” then?