Lesson Description

The "Arrays" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:

Melkey begins comparing Arrays and Slices. Arrays are defined with a fixed length and can only contain a single data type. For example, declaring an integer array of length 5 means the array can have at most five integers. This lesson also covers declaring multi-dimensional arrays.

Preview
Close

Transcript from the "Arrays" Lesson

[00:00:00]
>> Melkey: Let's quickly talk about arrays and slices. When individuals think of an array, they think of a list, and I think that's an appropriate definition. It stores a bunch of values, right? An array could be fairly versatile, it's a very powerful data structure. And slices might be a new terminology for some individuals, depending on their background.

[00:00:25]
For example, in Python, an array is dynamic. You can continuously append to an array, and Python just gives you a thumbs up and smiles all the way through. That's not necessarily the same case for Go and other programming language, okay? So If you want to declare a array, we're going to declare the name, so numbers are short form notation, and the syntax for an array is square brackets, and then the capacity or length of this array here, so five, right?

[00:00:54]
This is telling the compiler we expect this array to hold five values. The following argument or the following definitions, what type R can that array hold? In Go, we cannot hold multi types in an array, you can only have one array that holds ints, one array that holds structs, one array that holds strings, etc, etc.

[00:01:15]
So we can define it like so. So we have our numbers, it's gonna hold five, and we can just Populate these with ten, 20, 30, 40, and 50, okay? If you try to put 60, the compiler says index five is out of balance because it understands that this numbers array can only hold five ints so we're gonna adhere to that.

[00:01:35]
It's completely fine if we don't have the full five matching the full capacity, we can append to it or assign a more appropriate definition, be assigning that last slot to a value, but here we'll just declare numbers as the full array of integers, okay? And just for funsies, I know this can be annoying, but if I put a string, obviously, the composite cannot use string, all right?

[00:02:06]
>> Melkey: So that's an array, and arrays cannot change once it defined, it can only hold five, right? So I cannot add a six value to my numbers, or I cannot do numbers five=60. And there's no way you can change this. Once the array is defined and set, that's the only capacity you can do.

[00:02:26]
You can change values in the array, right? You can reassign the zeroth index, right? If I wanted to, to 60, that's all good, everyone's happy about that. But adding more capacity to the array is not possible, it's at definition time. Okay, we can print this array, we can do format, we can do print F, I'll show the format specifier for this.

[00:02:51]
We say this is our array, and this would actually be modulo V. We can put a new line and type in our numbers array like so. And see we have our array 10, 20, 30, 40, and 50. Okay, so we can access values of the array. So we can do like zero to access the zero value, we get the length of the array, right?

[00:03:13]
So we can do something like the keyword length, and type in numbers to get the length. So, there's a few ways we can bounce around our array. These are typical functions you would see in most implemented languages of how to navigate the array. So don't think there's any hidden secrets there.

[00:03:36]
If you want to access kind of the last value, maybe one other thing of any array, you can do something like format, just print the line. This is the last value. So if you wanna access the last value, you can do numbers access it, and do something like this.

[00:03:51]
So this hopefully would print 50, and there you go, there's the last value 50. Cool, you could do one other trick, I'll quickly show this. It's an array with size determined at initialization. I personally have never seen this useful ever but I'll just show you, numbers in it.

[00:04:16]
So, instead of putting a five here, you can explicitly do like this. It's like I personally don't truly see how useful this could be. But, instead of writing explicitly five if you know on initialization, again, how many values can be, you can just write them all out and just put the three ellipses there.

[00:04:40]
But again, for me, I don't see too much value in that. I'm gonna comment it out. Yeah, so those are arrays. You can have multidimensional arrays too, so like a matrix. I'll quickly show this before moving on. You can have a matrix and it goes column row. So, something like two, three, and then you can initialize it with the type, and then individual brackets here, right?

[00:05:06]
So, if I were to just have the function like so, this would not be valid Go, it would not be specified on the correct column index. You have to put an additional curly braces followed by the comma cuz the compile yells at you and then following another one like so.

[00:05:25]
We can put some values here, we can put one, two, and three, and then we can put four, five, and six. We can save this, and then right here, we can do format. We can use our Printf with our format specifier. This is our Matrix. I'm gonna do modular V, break the line and put the matrix like so.

[00:05:51]
>> Melkey: So there you go. All right, again, just some, I guess, derivatives on an array type, I just wanna make sure I show as many things I could think of with a raise.

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