Learn about Go slices that function as lists or arrays in other programming languages in this video. Discover how to create slices, how to access individual elements, and how to use slice notation.
- [Instructor] A slice is a sequence of items. All items in a slice must be of the same type. In line 10, we define a slice of strings. We specify the type by the square brackets, and then the type. This is a slice of strings. And then we can fill the slice with the curly brackets, and then a list of items that are there. So we have a slice of strings with bugs, daffy, and taz. And we can print it out. The %v verb will print out the slice as well.
To find the length of a slice, use the len built in function. You can access sliced elements with the square brackets. Indexing starts at zero, so printing loons will print daffy, and not bugs. And we can use slice notation on the slices. So in line 22, we are going to print everything from index one, so we'll print daffy and taz. Let's save it and run it.
And we see that we got the loons, and then three is the length, daffy is the second one, in index one, and we printed with the slice only the last part of the slice. I've pasted some code from the exercise files. To iterate over the elements in the slice, you can use the conditional for loop, as can be seen in line 26. We start with zero, up to the length of the loons, and then i++, print the loon.
However, in Go, we also have a range keyword, as we can see in line 32. If we do for i equal range of loons and print i, we will print the indexes of the slice, so we'll print zero, one, and two. If you want both the index and the value, we can use range with a double value context on the left side. So here in line 38 I have both i and name, and then I can print that the name is at a certain position.
If you want just the values, we have to use the underscore, as in line 44, because unused variables in Go are a compilation error. Let's save it and run. Right, and we see zero, one, two when we did the first range, and then bugs at zero, daffy at zero. And then at the end we print just the values. If you want to extend the slice, we can use the built in append function.
Append will add another element to the end of the slice. And if you're going to save this code. And if you run the code again, we can see that the slice contains bugs, daffy, taz, and also elmer from the append. There is another type in Go called array, and slices are built on top of arrays. However, in practice, you'll seldom use arrays, so we'll skip them here.
- Go basic types, conditionals, and loops
- Go functions
- Object-oriented programming with Go
- Error handling
- Concurrent programming with channels
- Testing and project management
- Networking with JSON and HTTP
Skill Level Beginner
Programming Foundations: Fuzzy Logicwith Erin Colvin41m 50s Intermediate
Getting started1m 34s
1. Getting Started
2. Go Basics
5. Error Handling
7. Project Management
Next steps1m 12s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.