Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.8 Arrays

Often we want to group together like pieces of data so that we can either iterate over them or treat them as a whole. In Go, like most languages, we have an array type to do just that. One of the limitations of an array in Go is that once you create it, you cannot change its size. But have no fear. There is a way around that as well, known as a slice. I’ll introduce both arrays and slices in this lesson.

Related Links

2.8 Arrays

Now is the time where it will definitely be very beneficial to go over a special data type. That you're going to find in many other programming languages, and Go is no different. In this case we're going to talk about arrays. Now arrays are simply a collection of pieces of data that are all of the same type given a specific size. So the way that we can specify arrays in Go is let's create a variable. And we're going to call this names and then we need to specify the type. Now we want to create an array of names that are all strings within Go. So the way that we do that is a little bit backwards than what you might be familiar with. But we're gonna specify open and close square brackets and within these square brackets, we're gonna specify a size. In this case, we're gonna say 2 to begin with, and then we need to give the type. So this is gonna be an array of string, so if I save that. And then want to print out names, and I run my application. You can see here I get open and close square brackets. Now it's not actually empty there are two values in there and those two values are the default values for a string type. Which are, the empty string. So let's modify this a little bit so we can see that in action. So the way I can actually set data within a certain index within an array is via the index notation. So I can specify names and then next to names, I'm going to give open close where brackets. And then an index and this index within the array is zero-based. So if I were to say the names zero is equal to Derek and save this and run again you're gonna see that I have an array that still has two values. The first value being Derek at index zero and the second value being the empty string at index one. Let's fix that, let's put something at index one. And that's going to be equal to John. Now if I were to rerun my application you'll see that I get Derek and John. Now we initialize this originally as having a size of two. But what if I wanted to make a little bit bigger? How do I do that? Well I could come in here and try to set the next a value equal to Bob, save that and run it. But I'm gonna run into an issue. This is going to be an invalid array index because it's out of bounds of the size that I initially created my array variable as. What does that mean? Well, in short terms that means that an array is actually a fixed size. Once I have created in array of a particular size there's no way for me to change it. I can change the values within the array at certain indexes but I can't actually extend or contract that array, I'm kinda stuck with that size. Now well that might seem a little bit limiting there is a way around. The way that we get around that limitation in the world of Go is by using something known as a slice. And a slice is well basically what it sounds like. I want to create a collection of data that basically functions and looks like an array but is of a slice of data. Of certain pieces of data within a given array. So let's start by creating a namesSlice slice. And I wanna set that equal to. We start by specifying there's a couple different ways to create this but we can start by specifying the name of an existing array. With open and closed square brackets just like we were doing our index notation with the exception that. Within there we're going to start by specifying a colon. Now this might look a little bit strange but bear with me for just a moment. Now I'm going to print my name slice. Let's clear the console here so I can rerun this. And as you can see here I get the same thing. It looks like an array, Derek and John. But what's really going on here in the background is. We're in essence creating basically a pointer to the existing names array. So when we created our slice we're not making a new copy of that names already. We're actually pointing to it and we're pointing to a range of data with within that a rate. What's the range? I don't see a range. Well the range is specified with in these square brackets with this colon. Now, the colon by itself might look a little strange. But it's the default for giving a range of values within the array. And creating a slice where the first value is the starting point, the starting index that I wanna create a slice from. And the second one is going to be the ending place but it's going to be exclusive. So if I wanted to create a slice that contained all the pieces of data found within my name's array. I would specify my starting index of 0, And my ending index exclusive, so in this case it would be 2. So let's save that, and if I were to rerun this you're gonna see that it continues to be Derek and John. But what if I don't want all of that data? Well I can be more explicit and say I only wanna go from 0 to 1 exclusive, and that's gonna give me just Derek. Or I could say maybe go from 1 to 2. And I could save that. And I could get just John. And I could do other combinations of that, where I could maybe leave the first one empty. And just do the second one as 2. And I'm going to get both things. And I could leave the first one as 0 and leave the second one empty. And I would once again get both things. And the reason for that is because if you don't supply a min or a max. It's going to give you the default of the starting point and the ending point. So you can be kind of flexible with that but you know what's the big deal? Why should I care about this? Well the reason you should care about this is because an array slice is a little bit more flexible. Now as I mentioned before, a name slice is pretty much like a pointer to an array. So if I were to actually come in here and change names one to be equal to Bob and save this and print my name slice. Now you're going to see I actually changed my name slice to be Derek and Bob and the same thing happens. For just names. And once again, as I said before, this is because this is in essence a pointer to an array. So what I can do now Is instead of having to deal with just a pointer to names. I can be much more flexible with my slice and actually append data to it. Now go supplies and append function for you so I can specify that and I can set my name slice equal to append. And the arguments for append are the name of the slice. So I want to append onto name slice and I can give a value I want to append, in this case I want to append Bob. So let's save that and then we'll specify printing out the name slice. Let's clear this and now let's rerun it and you can see I get Derrick, John and Bob. So what we've done now is we've created the slice that has kind of two things going on for it. It's a pointer to the existing data in names but it also has an additional piece of information Bob, but names itself is still just Derek and John. So it's kind of an interesting way to to handle some of the extensibility or being able to grow or shrink an array in sort of a roundabout way. One final thing I want to mention is that for this append function, you can actually add multiple arguments on the end. So I can put all sorts of other people up here. Save that, rerun. And, as you can see, I can continue to add things onto my slice. So there you have it, that's the basic concept in the world of Go when it comes to arrays. Creating them, assigning values, and getting values from them. But then also even though they're fixed, being able to extend them a little bit, make them a little bit more flexible, through slices.

Back to the top