1 00:00:00,290 --> 00:00:02,940 So far, we've only had to store single values, but 2 00:00:02,940 --> 00:00:06,730 it's much more common in computing to need to store an entire list of values, 3 00:00:06,730 --> 00:00:10,480 like items on a grocery list or phone numbers in a contact list. 4 00:00:10,480 --> 00:00:15,240 Go has a couple different structures that can do this, arrays and slices. 5 00:00:15,240 --> 00:00:18,790 Slices are used more commonly than arrays, for reasons we'll discuss shortly. 6 00:00:18,790 --> 00:00:21,148 But since slices are built on top of arrays, 7 00:00:21,148 --> 00:00:23,720 it's important to understand arrays as well. 8 00:00:23,720 --> 00:00:25,440 So we'll look at arrays first. 9 00:00:25,440 --> 00:00:27,740 An array consists of a list of elements. 10 00:00:27,740 --> 00:00:31,220 An array type is written with a number here in square brackets, 11 00:00:31,220 --> 00:00:33,700 followed by the type name. 12 00:00:33,700 --> 00:00:36,160 The number represents the size of the array, and 13 00:00:36,160 --> 00:00:39,090 the type is the type of elements that the array will hold. 14 00:00:39,090 --> 00:00:41,730 All of the elements in an array need to be of the same type. 15 00:00:41,730 --> 00:00:45,820 So for example, you can't assign a number to this array of strings. 16 00:00:45,820 --> 00:00:49,430 You can assign to an element of an array by including 17 00:00:49,430 --> 00:00:53,530 the variable that holds the array, followed by 18 00:00:53,530 --> 00:00:58,615 square brackets with the numeric index of the element you want to assign to. 19 00:00:58,615 --> 00:01:02,350 0 is the first element, 1 is the second element, and so forth. 20 00:01:02,350 --> 00:01:04,932 You can assign to the element using the equal sign, 21 00:01:04,932 --> 00:01:07,225 just like you would with any other variable. 22 00:01:07,225 --> 00:01:10,952 And you can access the value that you've stored in that element, 23 00:01:10,952 --> 00:01:14,136 just by putting the variable name that holds the array, 24 00:01:14,136 --> 00:01:17,405 followed by the square brackets with the element index. 25 00:01:17,405 --> 00:01:21,411 So for example, we assign the strings April, May, and 26 00:01:21,411 --> 00:01:23,695 June to the months array here. 27 00:01:23,695 --> 00:01:28,485 And then we print them out down here one at a time, April, May, and June. 28 00:01:29,725 --> 00:01:32,785 You can see that in the output down here at the bottom. 29 00:01:32,785 --> 00:01:38,072 Same thing for the sales by month, we assign float64 values to the first, 30 00:01:38,072 --> 00:01:42,050 second, and third elements of the salesByMonth array. 31 00:01:42,050 --> 00:01:45,080 And then we print them out one at a time down here at the bottom. 32 00:01:47,207 --> 00:01:50,262 If you know what the contents of an array are going to be beforehand, 33 00:01:50,262 --> 00:01:53,900 you may find it's more convenient to populate it using an array literal. 34 00:01:53,900 --> 00:01:56,740 You do that by putting the values the array is going to hold 35 00:01:56,740 --> 00:02:00,980 here in curly braces, following the array declaration. 36 00:02:00,980 --> 00:02:04,910 So this code here has the exact same code as the lengthy previous code. 37 00:02:04,910 --> 00:02:09,393 It assigns the values April, May, and June to the array months and 38 00:02:09,393 --> 00:02:13,850 then several float64 values to the salesByMonth array. 39 00:02:13,850 --> 00:02:18,530 You can get the length of an array by using the built in len function, 40 00:02:18,530 --> 00:02:19,820 short for length. 41 00:02:19,820 --> 00:02:24,500 So, we could use a single print line statement within a loop to print out 42 00:02:24,500 --> 00:02:26,717 the the entire contents of the months and 43 00:02:26,717 --> 00:02:31,410 salesByMonth array if we just get its length using the length functions. 44 00:02:31,410 --> 00:02:33,870 So, let's create a for loop. 45 00:02:33,870 --> 00:02:39,070 We'll set up an i index variable to keep track of where we are within the array. 46 00:02:39,070 --> 00:02:43,729 We'll start that at the index 0, and we'll continue looping while 47 00:02:43,729 --> 00:02:47,600 the value in i is less than the length of the months array. 48 00:02:49,640 --> 00:02:54,190 For our post statement, we'll just increment the index variable. 49 00:02:54,190 --> 00:02:59,384 And then we'll just include a single fmt.Println statement, 50 00:02:59,384 --> 00:03:04,095 which prints the value from months at the current index and 51 00:03:04,095 --> 00:03:08,245 the value from salesByMonth at the current index. 52 00:03:08,245 --> 00:03:16,370 Save that, run it, and we get the same result. 53 00:03:16,370 --> 00:03:17,150 The form of the for 54 00:03:17,150 --> 00:03:21,000 loop with the init condition and post statement is pretty brittle though. 55 00:03:21,000 --> 00:03:26,120 Suppose we'd accidentally changed the condition to i is less than or 56 00:03:26,120 --> 00:03:27,810 equal to the length of months. 57 00:03:27,810 --> 00:03:31,570 Well, the length of the months array is 3, but if we try and 58 00:03:31,570 --> 00:03:35,130 use an index of 3, that's out of bounds for the array. 59 00:03:35,130 --> 00:03:37,050 Let's try running it like this. 60 00:03:37,050 --> 00:03:41,357 And you see we get an error, saying that the index we tried is out of range. 61 00:03:41,357 --> 00:03:45,790 This is why Go offers another form of for loop. 62 00:03:45,790 --> 00:03:48,772 We can declare i and month variables and 63 00:03:48,772 --> 00:03:53,210 say that they'll be receiving values from range months. 64 00:03:53,210 --> 00:03:57,476 We provide the name of an array here, use the range keyword, and 65 00:03:57,476 --> 00:04:01,979 we specify the names of variables that will be receiving the current 66 00:04:01,979 --> 00:04:05,950 index of the current value and the value itself. 67 00:04:05,950 --> 00:04:09,990 This will loop over the array, assigning the index and the value for 68 00:04:09,990 --> 00:04:12,280 each value the array contains. 69 00:04:12,280 --> 00:04:16,736 So down here in the Println statement, we can, instead of saying months, 70 00:04:16,736 --> 00:04:20,783 at the current index, we can just put the month variable's value. 71 00:04:20,783 --> 00:04:27,557 And then we can continue to access the salesByMonth array at the current index, 72 00:04:27,557 --> 00:04:31,919 since those values correspond to the month names. 73 00:04:33,110 --> 00:04:34,210 Save this and try running it. 74 00:04:36,330 --> 00:04:38,830 And you see that we get the same output as before. 75 00:04:38,830 --> 00:04:40,810 You'll find this form is generally much safer and 76 00:04:40,810 --> 00:04:45,000 less error prone than using indexes to loop over the values in an array. 77 00:04:45,000 --> 00:04:47,470 If you don't need either the index or the value, 78 00:04:47,470 --> 00:04:50,110 you can omit them using the blank identifier. 79 00:04:50,110 --> 00:04:53,430 So for example, if we didn't need to print the salesByMonth, 80 00:04:53,430 --> 00:04:58,230 we could just omit the index by using the blank identifier there. 81 00:05:01,630 --> 00:05:03,980 Whoops, slight mistake there. 82 00:05:03,980 --> 00:05:07,630 Because now I'm not using the salesByMonth array at all. 83 00:05:07,630 --> 00:05:09,690 I can just comment that out, and it should compile. 84 00:05:11,810 --> 00:05:16,920 There we go, and we have just the names of the months now, April, May, and June. 85 00:05:16,920 --> 00:05:18,550 The length of arrays is fixed. 86 00:05:18,550 --> 00:05:19,900 They can't grow or shrink. 87 00:05:19,900 --> 00:05:24,290 For example, if we were to try to add a fourth string to the months array, 88 00:05:24,290 --> 00:05:30,900 let's say months index 3 = July. 89 00:05:30,900 --> 00:05:32,280 Try saving that and running it. 90 00:05:33,870 --> 00:05:38,500 We get invalid array index 3 (out of bounds for 3-element array). 91 00:05:38,500 --> 00:05:43,200 This limitation is the main reason arrays are rarely used directly in Go programs. 92 00:05:43,200 --> 00:05:47,600 It's often important to be able to add new values onto a list after its creation. 93 00:05:47,600 --> 00:05:51,980 This is why Go provides an easy to use alternative to arrays, slices.