Golang Tutorial #11 - Switch Statement

"WEBVTTKind: captionsLanguage: enhello everybody and welcome back another golang tutorial so in this video we're gonna be talking about the switch statement now the switch statement is very similar to the if else if and else structure we've seen previously this works a little bit different it's more to accommodate a specific case and make our code a little bit cleaner but I'll show it to you here and you will see very quickly why you might use a switch statement versus an if else if and else so I'm just gonna code one out to start and then we'll just walk through the syntax and talk about how it works so the key word to start a switch statement is switch I'm gonna say switch I'm gonna put some variable here and this is the variable that I want to check the values for so the idea behind a switch is we're not really gonna be doing comparisons in terms of like greater than less than we're just gonna check if this variable here is equal to a bunch of different things and if it's equal to any of those things then what we'll do is we'll execute some code based on what it's equivalent to so I'll say switch ans and again remember that what I'm doing with ans is I'm just gonna be checking the value of an S and see if this is equal to what I'm about to type here so if I do something like case 1 what this says is okay the first case in our switch statement is if an S is equal to 1 if it's equal to 1 what I'll do is whatever is indented right after that : in this case so I would print Ln let's say 1 okay so this is the syntax case 1 this is the value that we want to check you could you know put a string here if the variable above was a string it doesn't have to be a number you put : then what you want to do indented underneath and if we want to do multiple things that'll be fine we can print them out like that so maybe I'll do one like I could just do like one like that just show you this first line and both of these will print out since they're indented underneath this : now to add another case I can do case two fmt dot print line let's just print two like that and then I could go case 3 and we can do as many cases as we want so you can see that this is nice for kind of organizing your code and making things really clean it's pretty easy to see what's gonna happen in each case because you can just look right here and see what the value is and what's gonna happen after that value you know occurs or is equal to that and the last thing to show here is the default statement so this is an optional statement but what this says is assuming that this variable is not equal to any of these cases go ahead and run the default so I'll say FM t dot prints line not a case so that pretty much is like the else statement on the if-else l if structure now there's a few more things to talk about but let me just print this out and show you what happens say if an S is equal to one so let's go go run tutorial go we get the value 1 1 right so it just prints out the two things that I showed you here and then if we set this equal to let's skip to let's just go to 3 which is not one of the cases go run tutorial go we get not a case now what we can also do is we can check for multiple cases on the same line so say that I want to do this I want to print out one and one like both of these lines here if this is equal to one or negative one then all I have to do is rather than making a new case and repeating this code is I just do all of the things that I want to check for separated by commas after the case right so I write case and now I'm checking if it's 1 or negative 1 if it's either of those then I'll do both of that so I'll prove that's you let's go negative 1 and have a look here and we get 1 1 now this works the same with other variables as well that we've seen before so I cannot do something like this right I can't put a string here and you'll see if I save this what's gonna happen just get the squiggly line popping up and it says that there's a midst mismatch type because well this is an integer and this is a string and we can't check the equivalence between the integer and a string so keep that in mind that the cases need to be the same we can do as many cases as you want you can do a default case and there's one more paradigm to writing the switch statement that I'll show you which doesn't actually involve putting a variable here at all now this is a little bit like why would you want to do this but I'll show this to you anyways there's some uses for it if you just switch with no variable up here then what you have to do when you write your cases is directly reference the variables so I say if case ans greater than 0 right and then here I can do it I wanting an FM t dot println greater than zero I can do another case so case an s less than zero or I guess I could just do the default case then whatever you want FM t dub print line less than zero and then finally I guess you would add a default case that says FM t dot print line zero so this is another example of what you can do with a switch statement again right like what's kind of the point of doing this versus if-else there's not really any point of doing this versus if-else for the example I've shown but you know if you have a bunch of cases that are related and you just want to write a switch statement rather than if Ellis like maybe that just looks better to you or for some reason in your specific case it makes sense to do this then you can write a switch statement but this is valid this is totally valid this is just saying okay we're not gonna put a variable here but what that means is I have to directly reference the variable which kind of gets rid of the point of a switch name in anyways but you know sometimes you might want to do something like that okay so I'm just gonna run this and I'll just show you that this does work I'm not making this up let's have a look less than zero and that's pretty much it for the switch statement there's not much more I could think to show you if you guys think I forgot anything let me know but with that being said I hope you enjoyed if you did make sure you leave a like subscribe and I will see you in the next video\n"