Slices

There is a very obvious limitation in Go's array data structure—you must specify the size whenever you declare a new array. In real life, there are numerous scenarios where we will not know the number of elements to expect beforehand. Almost every modern programming language comes with its own data structure to address this requirement. In Go, this special data structure is called a slice.

From a practical point of view, you can think of slices as simply dynamic arrays. From a syntax point of view, slices look very similar to arrays, except that you don't need to specify the size. Here is an example:

var mySlice []int

As you can see, slice declarations are very similar to array declarations, except for the fact that you don't need ...

Get Hands-On Full Stack Development with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.