1 00:00:00,410 --> 00:00:03,100 To choose between several different options, using if and 2 00:00:03,100 --> 00:00:07,030 else states, you'd have to nest some several deep, which can be hard to read. 3 00:00:07,030 --> 00:00:11,150 So like most programming languages, Go offers a switch statement. 4 00:00:11,150 --> 00:00:14,990 You specify the expression you wanna switch on, case statements with 5 00:00:14,990 --> 00:00:20,460 possible values for that expression, and then code that should run for each case. 6 00:00:20,460 --> 00:00:22,720 You can also add the default case at the end, 7 00:00:22,720 --> 00:00:25,520 that will run if none of the other cases match. 8 00:00:25,520 --> 00:00:30,720 So up here, we've initialized the doorNumber variable with the value 2, 9 00:00:30,720 --> 00:00:32,240 the integer 2. 10 00:00:32,240 --> 00:00:35,430 And we're going to switch on the value that's in doorNumber. 11 00:00:35,430 --> 00:00:38,968 So we've got case statements here, for possible values of doorNumber. 12 00:00:38,968 --> 00:00:44,662 If doorNumber was set to 1, we would print the string, a new car! 13 00:00:44,662 --> 00:00:47,672 But up here, it's actually set to 2, so 14 00:00:47,672 --> 00:00:51,550 it's going to match this case here, case number 2. 15 00:00:51,550 --> 00:00:57,230 And it means that his code here will run, it will print the string, a llama! 16 00:00:57,230 --> 00:01:01,708 So if we try running this down here, we'll see, You win a llama! 17 00:01:01,708 --> 00:01:04,954 Let's try going up here into the code and setting it to 1, so 18 00:01:04,954 --> 00:01:07,030 that we can look at the other case. 19 00:01:07,030 --> 00:01:08,510 Try running this again. 20 00:01:08,510 --> 00:01:10,458 This time, you can see we won a new car. 21 00:01:10,458 --> 00:01:14,186 And if we set it to some completely different value, 22 00:01:14,186 --> 00:01:16,850 let's say we pick door number 99. 23 00:01:16,850 --> 00:01:20,828 In that case none of the cases here will match, and 24 00:01:20,828 --> 00:01:24,530 therefore the default case will run instead. 25 00:01:24,530 --> 00:01:26,205 And it will print, You win a goat! 26 00:01:27,849 --> 00:01:30,030 Let's change this back to doorNumber 1, up here. 27 00:01:31,040 --> 00:01:34,620 In some languages, the case after the selected case will also run, 28 00:01:34,620 --> 00:01:37,375 unless you insert a break statement at the end of the case. 29 00:01:37,375 --> 00:01:40,350 In Go, only the selected case runs by default. 30 00:01:40,350 --> 00:01:43,880 So, if we select doorNumber 1, only doorNumber 1 runs. 31 00:01:43,880 --> 00:01:46,962 But you can run the next case as well, 32 00:01:46,962 --> 00:01:51,460 if you explicitly include the fallthrough keyword. 33 00:01:51,460 --> 00:01:55,569 So lets type fallthrough here, and try re-running it. 34 00:01:55,569 --> 00:01:58,527 And now, we win both a new car and a llama. 35 00:01:58,527 --> 00:02:01,871 That ends our tour of control structures in Go. 36 00:02:01,871 --> 00:02:03,791 Go's a very simple language, so 37 00:02:03,791 --> 00:02:08,210 you've actually learned a substantial part of its syntax already. 38 00:02:08,210 --> 00:02:10,930 The next stage is going to be a bit longer than this one. 39 00:02:10,930 --> 00:02:13,760 We're going to look at the different ways Go can store data, and 40 00:02:13,760 --> 00:02:15,030 there are a lot of them. 41 00:02:15,030 --> 00:02:18,240 We'll learn about slices, maps, structs and more. 42 00:02:18,240 --> 00:02:19,790 Learning them will take a bit of work, but 43 00:02:19,790 --> 00:02:23,800 when you're done, you'll have a variety of ways to simplify your programs. 44 00:02:23,800 --> 00:02:24,840 See in the next stage.