From the course: Learning Go (2021)
Unlock this course with a free trial
Join today to access over 24,400 courses taught by industry experts.
Manage ordered values in slices - Go Tutorial
From the course: Learning Go (2021)
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…
Contents
-
-
-
-
-
How memory is allocated and managed3m 29s
-
(Locked)
Reference values with pointers4m 18s
-
(Locked)
Store ordered values in arrays3m 17s
-
(Locked)
Manage ordered values in slices4m 43s
-
(Locked)
Store unordered values in maps5m 54s
-
(Locked)
Group related values in structs4m 41s
-
(Locked)
Solution: Convert a slice of strings to a map2m 3s
-
-
-
-
-