Slices are a key data type in Go+, giving a more powerful interface to sequences than arrays.
Unlike arrays, slices are typed only by the
elements they contain (not the number of elements).
To create an empty slice with non-zero length, use
make. Here we make a slice of
strings of length
3 (initially zero-valued).
We can set and get just like with arrays.
len returns the length of the slice as expected.
In addition to these basic operations, slices
support several more that make them richer than
arrays. One is the builtin
returns a slice containing one or more new values.
Note that we need to accept a return value from
append as we may get a new slice value.
Slices can also be
copy’d. Here we create an
c of the same length as
s and copy
Slices support a “slice” operator with the syntax
slice[low:high]. For example, this gets a slice
of the elements
This slices up to (but excluding)
And this slices up from (and including)
Slice literals in Go+ style
Slice literals in Go style
We can declare and initialize a variable for slice in a single line as well.
Slices can be composed into multi-dimensional data structures. The length of the inner slices can vary, unlike with multi-dimensional arrays.
Next example: Maps