Chapter 6. appending issue: Slices
We’ve learned we can’t add more elements to an array. That’s a real problem for our program, because we don’t know in advance how many pieces of data our file contains. But that’s where Go slices come in. Slices are a collection type that can grow to hold additional items—just the thing to fix our current program! We’ll also see how slices give users an easier way to provide data to all your programs, and how they can help you write functions that are more convenient to call.
There actually is a Go data structure that we can add more values to—it’s called a slice. Like arrays, slices are made up of multiple elements, all of the same type. Unlike arrays, functions are available that allow us to add extra elements onto the end of a slice.
To declare the type for a variable that holds a slice, you use an empty pair of square brackets, followed by the type of elements the slice will hold.
This is just like the syntax for declaring an array variable, except that you don’t specify the size.
Unlike with array variables, declaring a slice variable doesn’t automatically create a slice. For that, you can call the built-in
make function. You ...