Home
Sign in Buy now

Golang Language Fundamentals

Slices, maps and looping

Revision

Summary

In this episode, we explore Go's data structures for working with sequences of elements, including arrays (fixed size) and slices (dynamic size). We learn how slices are built on top of arrays and can grow dynamically using the append function. The episode also covers maps as key-value data structures and demonstrates various ways to loop through these collections using Go's for keyword with different syntax patterns.

Transcript

To work with a sequence of elements in Go, we can use either a slice or an array. Slices are dynamic in size, while arrays are fixed in size. In Go, it typically reads for a slice, but I want to show you an example of both. So let's create an array here, let's call it accounts and say this variable can have two strings. And let's just assign some values, let's say we have our Alice account and our Bob account here. Now, it's important to note that the size or capacity of this array makes it into a distinct type. So what that means is that if we had another array that was also holding strings, but let's say it could have three strings instead of two, we could not combine these two because they are technically distinct types. Now, the slices or slice version of this would be, let's call it accounts slice. And instead of specifying the capacity, we leave that empty and we are again going to be using a string here. And I'm going to add Alice and Bob to this slice again. Now, as I said, slices are dynamic and can grow in size so we can continuously add elements to a slice. Slices are built on top of arrays. And this simply means that whenever we create a slice, it gets a default set capacity. And once that capacity has been reached, this slice will be reallocated to a larger underlying array by the Go compiler. Now, to add to this slice, we could say, here, let's say account slice. Then we're going to use the append function where we are going to again say we want to append to account slice. And let's add Charlie to the slice. So now our account slice have three elements, Alice, Bob and Charlie. There's also built in support for maps, which is a simple key value data structure. And to create a map, we are going to specify the map keyword and then the data type of the key and then the data type of the value. So let's create a new map here and let's call it user account balance. And we're going to say the key is going to be a string and the value is going to be int64. In here, let's say we have Alice and Alice has an account of 100 and Bob has an account of 50. We could also create an empty map and then add to it so we could jump up here and say, let's call it user savings account. And that is going to be equal to a map string and int64. Hi, Future Morten here. I'm currently missing a step in the example I'm showing you on screen right now, and that is to initialize the map. If you were to run the example right now, it will cause a panic. And that's because we haven't initialized the map. So to do that, go to the variable we are creating for the map. And then you simply wrap the map part in a make function and then set it to zero. And that way we can actually run the examples on the screen. Okay, back to the video. And then down here, you could now add to this map by saying user savings account. Let's create a savings account for Alice. Alice has 50. And then we could repeat this for Bob and say Bob has a savings of 200. So this is just different ways that you can work with maps and add data to it. Finally, we can loop through these data structures and I'm just going to show you some different ways. But technically we only have one real way of doing loops in Go with the for keyword. So let's say we wanted to loop through all of the accounts of the accounts array. We could say for i equals zero and then as long as i is less than the length of the accounts array, then we're going to increase i and for each iteration we could say something like fmt.print. Say account index and owner. Then we can specify the index and we can specify the accounts. You can also say for key, value equals range and let's range over the user account balance map instead this time and again here we can print out the values. So instead of saying account index, we could say something like the ID is going to be string and then we will say the balance is going to be a value. And then we say key and then we say value. So for each iteration when we loop over the user account balance, we get the key and then we get the value. Now we can also use the index directly. So if you were to say something like index, account and then we loop over the accounts slice here, then again, let's just print out these values. So we can again say index and we then have the owner. So this is going to be the index and this is going to be the account. Finally, we can also say for i, account range and then accounts at index. And then we just provide the index. This is some of the more modern versions of looping that we could use. But this is how you work with slices and arrays and maps and how you can loop through and get the values out if you ever need to do so.

Episode Notes

There is an initialization error in the example with creating the map. It should be: var userAccountValue = make(map[string]int64)

Get the Full Course

Unlock all episodes and get lifetime access to course updates.

What others say

"With this course, I'm learning Golang + datastar while building my personal blog hitting several birds with one stone. It's a fantastic course."

Testimonial from ignacio
Ignacio Barceló
Bought Early Access

Video Completed!

Great job finishing this episode

Up Next

Golang Language Fundamentals
Structs and interfaces