Transcript from the "Arrays" Lesson
[00:00:21] And if we're talking about Go, that line 9 in the Go box, is initializing an empty array, and our var scores is set to a type of float64 with five elements in it. So the hard bracket five float64 is actually a type definition, similar to string, boolean, etc.
[00:00:39] But that type definition has to include how many things are in the array, how many elements it contains, and exactly what type those are, all of which have to be the same type. So eventually, the array in Go could only contain floats and it could only have five elements.
[00:00:55] So it's a pretty significant difference when working with these two structures between the two languages. A really important thing to note as well is similar to, if we were to compare a string to an integer, Go would tell us that those are not the same things. If we were to compare two arrays of types 1 has five elements with a float64, the other has six elements of a float64.
[00:01:19] Those will come back as two very different types, completely different type signatures and different allocations in memory. So we are gonna hop over to either the Go playground or a Go file and talk about how these arrays get initialized, and why they're important in Go. So we're gonna look at line 9 and copy this guy over into a Go file, and just print it out as is.
[00:01:41] So I'm gonna move over to our code editor. I'm gonna create a new file, Called arrays.go. Set up my initial Go template. And then here, I'm going to copy over our scores array, so it is five elements, all of which are type float64. If I print line scores And run our program, go run section 4 code arrays, you'll see I have allocated five elements within our array, all of which already have default values of 0.
[00:02:33] And a reminder back to the beginning, when we have a default value that's a float, it's going to pop in a zero for that value behind the scenes. And so, Go is kind of setting aside enough memory for us to eventually add values into our array, but for now it's putting a zero in there.
[00:02:52] Okay, so if we wanted to find values and populate our array, one of the methods we could use is to individually target each index and then populate that value manually, which is not an ideal way to define an array. Instead we can use a more concise syntax where we are establishing that we are setting up a variable called scores which has a type signature of five elements of float64.
[00:03:14] And then on the right hand side, we can use that type signature and then instantiate each of the new values within curly braces. The comma separated values within those curly braces will be allocated to each respective index in our array. As we've seen with using shorthand syntax and variables, because we were being so specific on the right hand side, Go will infer that if we start with a variable called scores and then use a colon equal sign, it will assume we want five floats on the right hand side.
[00:03:47] And then a little similarly here, if we have explicit initial values on the right, Go can also infer that you know you want five things in this array, there are five things within the curly braces. So the dot dot dot within the higher brackets can replace that explicit number.
[00:04:06] Cool. So I'm gonna hop back into the code editor just to demonstrate that by using range, we can iterate over this array similarly to how we did the string example earlier. Okay, so when we're back in our arrays.go file, we're gonna create a for loop to iterate over an example of an array.
[00:04:25] So we have our var scores, which is set to five elements of float64. Which we can refactor to just have scores, and then set that equal to An array of values. I'm gonna use an arbitrary array of values for this particular example. So I'm just going to do 1, 2, 3, 4, 5.
[00:04:52] And then our for loop we can say for each index, which I'm not gonna use, and the value of that array. We can use the range keyword to iterate over our scores. And then print line value. If we run our code here, we'll see 1,2,3,4,5. Had we included our index here Without using that index, we would see, I declared a not used error.
[00:05:25] So to fix that, we'll make sure to implement the underscore.