From the course: Learning Go

Manage ordered values in slices - Go Tutorial

From the course: Learning Go

Manage ordered values in slices

- [Instructor] A Slice in Go is an abstraction layer that sits on top of an array. When you declare a slice, the runtime allocates the required memory and creates the array in the background but returns the slice. Like arrays, all items in a slice are of the same type, but unlike arrays, they're re-sizable, and they can be sorted quite easily. In this example, starting in my main.go file in the practice directory in this branch, I have my variable called colors. And I'm explicitly declaring it as a variable with three items. And when I run the code, I get the expected output of red, green, blue. Now, this is an array, not a slice. And the reason is because I set an explicit number of items. I set the length. If I remove that number, now it's a slice and you can add and remove items, sort it, and so on. If I run this code, it looks exactly the same but now I'll add an item. I'll start with colors equals, because I'm reassigning the variable, and I'll call a built-in function called append. I'll pass in colors as the first argument and a new string, this time purple, as the second one. I'll copy and paste my output down here, Save and run, and I see that I've successfully added an item to the slice. To remove items from a slice, also use the append function, but this time indicate range. Two numbers separated by a colon wrapped in parentheses. It'll look like this. Colors equals, append, I'm passing colors as the slice I'm effecting, and then I'll start with a pair of brackets. I'll start with one colon, length of colors. And then I'll put the slice and I'll see that the first item has been removed. That's because that range is saying, start with the item in array index one, that's the second item, and then also go get all the other data to the end of the slice. If you leave out the first number, the starting index, it defaults to zero. I'll copy and paste this code and eliminate that first value and start with colon then the length of the colors that this time I'll say length of color is minus one. And this time I'm deleting the last item in the array. You can also declare a slice with a type and an initial size with the built-in make function. Make takes three arguments, the type of the slices items, the initial length and an optional capacity that caps the number of items, that the slice can contain. So I'll start with numbers this time and I'll initialize this variable with colon equals, then I'll call the built-in make function. I'll pass into type as an array of int, and then an initial size of five and a cap of five. I'll create the first item with numbers, colon zero equals 134. Then I'll duplicate this line of code a number of times, I'll change the indexes and I'll change the values. And these could be any numbers you like, mine are completely arbitrary. And I'll run that code and see the result. If you leave out the capacity by removing that last argument the code will still do exactly the same thing as before, but now you can add items. I'll say numbers equals append, numbers and some other number, and then once again, I'll put the slice and see that the number has been successfully added. You can also sort a slice using the sort package. In my import statement, I'll add the sort of package here. Now I'll come back down to the bottom of the code and I'll say sort dot int. And notice that there are functions available for various types. There's a Float64s, a Float64Slice and Intslice and so on. I'll sort the integers, and then I'll output the result, and now they're in numerical order from lowest to highest. You can find plenty of more information about other sorting techniques, in the documentation for the sort package. Including sorting slices of structures by the structures member values, and you can also sort your own user defined data collections.

Contents