Build Go Apps That Scale on AWS

Arrays & Slices std library

Melkey

Melkey

Twitch
Build Go Apps That Scale on AWS

Check out a free preview of the full Build Go Apps That Scale on AWS course

The "Arrays & Slices std library" Lesson is part of the full, Build Go Apps That Scale on AWS course featured in this preview video. Here's what you'd learn in this lesson:

Melkey demonstrates the differences between Arrays and Slices. Arrays have a fixed length assigned when they are declared. Slices are more flexible because their length can change dynamically. This flexibility does require more memory.

Preview
Close

Transcript from the "Arrays & Slices std library" Lesson

[00:00:00]
>> Right, so let's go make a new folder called loops in here, main.go. You know the drill. Package main, func main, awesome. All right, so in go looping, you have to loop over something. What do you loop over? There's arrays. We know what arrays are. They're lists, they can store value, all right?

[00:00:23]
And arrays in Go basically can be, they're fixed in size. So when you declare an array, you are declaring the size right away with an array. So an example of an array in the syntax is, let's say we create an array of animals. And we can infer the type.

[00:00:40]
And the syntax to create an array is [], and then, what is this gonna store? So let's say it just stores 2, and we're gonna put string, okay? And basically, in Go, you cannot have mixed types in arrays. So this is gonna be an array that has two strings in it, all right?

[00:00:59]
And if you wanna add a value to to our array, you can do this equals, let's say, dog. Awesome, and then if you want to do another one, it's cat. We have an array of dog and cat. If you want to put another animal in the second index, it's gonna yell at us.

[00:01:18]
Sad arrange, so go strict, Go knows what you're trying to do. And arrays are very good in terms of memory and efficiency. Because at the time of declaration, you're already assigning all the memory it is going to need, all right? Now, there is a con to this, and that comes in the form of flexibility.

[00:01:40]
We can't append a new value into our array. That's not how arrays work. So there's trade off. They're useful but they're not flexible. So use them accordingly, right? And we can just print this format print line, let's just print a line of animals, right? And then we can run this, go run, go into our loops, main.go.

[00:02:02]
Ta da, dog, cat. Cool, we can reassign them, it's all good, nothing too crazy. If you don't like this method of indexing what value, cuz if you have a hundred or something, can get annoying. Another maybe easier way, you can just go in here and declare them at start.

[00:02:17]
So, dog, cat, okay? And if you run this, it's the exact same thing. And you can modify the string that's in that index. So those are arrays. However, there is a different native data structure in Go, let's go ahead and just delete this, and that's called a slice.

[00:02:36]
And a slice, if you guys can guess, it is a flexible, dynamic set sized, align essentially. It can be expanded, you can append things to it, you can delete elements from the slice because it's flexible, it's dynamic, again, costs more memory, yada, yada, yada. In the syntax, it's just the same thing.

[00:02:56]
String, like this, but we do not assign a value in those square brackets, all right? So it's just anything, so now Go knows, okay, this is a slice, let's rock and roll. Looks very, very close to an array, all right? And let's just append our values in here.

[00:03:13]
Let's do dog, cat, cool, awesome. And now, because it's a slice, we actually get introduced to some helper functions from Go. Let's say want to add a new animal, a moose, we can do animals, and we can do append, so a built-in method, an append takes in the slice and animals and a type that we wanna append to it.

[00:03:36]
So I'll put moose. Okay, format print line, animals. Save this. If we run it, dog, cat, moose. We got a crazy zoo, a Canadian Zoo, okay? This is cool. If you want to delete a value from a slice, there's no pop method, there's no like, remove this index method in Go.

[00:03:59]
And prior to Go 121, removing an element from a slice, it's still the case, but you basically create two slices from the original slice and then append them together. I swear, that's how you would move an element in slice in Go, and that's still how it's done. But in Go 1 21, they introduced In a helper package, a thin wrapper around that logic, we're gonna explore that, called slices.

[00:04:27]
If you don't have Go 121, don't worry, it's not a big deal. We're gonna go through it as well. But basically if you do have slices, what you do is you have to re-declare the value in animals, if you do slices.delete, put in the animal slice, and like here, you have to put a range.

[00:04:47]
You give it the range, the indexes of where you want to remove the element from your slice. So let's say we wanna remove our dog, really didn't wanna ever say that sentence out loud, but we'll put the 0 and 1, basically target the range from the first element that we're presenting, which is 0, up towards 1.

[00:05:04]
And if you print animals, we should get cat, moose, boom, cat, moose. I'm more of a dog person, but that is what the delete method does. And let's take a little peek into this method, all right? Don't worry about everything but this line right here, this is literally how you would run a new slice or create a new slice by removing element before the slices package.

[00:05:32]
You're basically calling append method on the existing slice two times with a range i and j, and then you have this dot dot dot notation. This dot dot dot notation in Go is called a variadic function, which basically means it can accept any number of arguments in a slice formation.

[00:05:50]
And so what's happening is append is taking our first element here, our first slice of the array, or our first portion of the slice, and we're saying, append to it every element after the range j. Could it work? Yeah, sure, once you do it, once you got it.

[00:06:12]
That's why I think the slice package is a little nice, cuz we as just the phasing developers, all we have to do is write delete and kind of handle it. So it's a little bit more manageable, it's the best, eh, but still works.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now