Summary
In this episode, we explore control flow in Go programming through if-else statements and switch statements. We cover basic if-else syntax, the importance of limiting nesting to two levels for code readability, and shorthand forms for checking conditions. We also examine switch statements as an alternative to multiple if-else conditions, demonstrating how to use cases, default behavior, and the automatic break feature that prevents fall-through unless explicitly specified with the fall-through keyword.
Transcript
It's typically helpful to be able to control the flow of operations based on certain criteria or conditions when you're programming in Go. We can either choose between a classic if-else statement or a switch statement. An if statement in Go looks like this: let's say we have the API key variable from last episode. We will say if API key equals to constant and super secret, and then whatever we have inside this bracket will get executed if this statement evaluates to true. Now we could add our else statement as well and just say if the first condition doesn't evaluate to true, then execute whatever is in the else statement. Finally, if we have multiple conditions we would like to check for, we could use an else if. So you can say else if, let's say API key is equal to not super secret, and then we will do something like this. And then again, if that evaluates to true and the first one does not, then we execute whatever is in these brackets. And if both of these evaluate to false, then we execute whatever is in the else statement. You can nest these as much as you want. However, a Go developer would tell you to never go beyond two levels, as this can lead to code that's hard to read and reason about. Finally, I want to show you a more shorthand form of doing an else if statement, and that is really handy whenever we only want to check a certain thing but we don't really want to use that thing later on in the program. So we can say if error equals true, let's say we have a function that validates the API key, so validate key API key here, and then if error does not equal null, then execute whatever is inside of these brackets. We haven't touched upon errors yet, we will do that in an upcoming episode, but the reason why this works here is that errors are values, so we could treat them as such. Instead of using an else if statement to check for multiple conditions, we could instead use what is known as a switch statement. And a switch statement simply looks like this: we specify the switch keyword and then whatever we want to check something about, so we say API key here. Then inside the brackets we can specify cases where we want to do something. So if we mimic what we had above here, we can say case constant and super secret, or another case was not super secret. And then finally we could say if none of these cases evaluated true, then we have a default case that will trigger if none of the other cases are evaluated to true. So we will simply have the logic after the colon here. Sometimes we might have situations where we want to do the same thing across multiple cases. So again, if we take our API key here, we could add another one and say if it's either constant and super secret or just super secret, then this case here should trigger. It's important to note here that the default case is not a requirement. Also, each case would automatically break if it matches the expression. There is no fall-through behavior as you might see in other languages. To have fall-through behavior where even if something evaluates to true that it continues to check, we will have to add the fall-through keyword. Finally, we don't even need to specify an expression. We could just say switch and then have, let's say we just grab the case from up here, let me just clean it up, and then we can just specify our cases directly. So API key equals to constant and super secret, or API key equals to not super secret, and so on and so forth. This is really, or this is particularly helpful whenever you have complex conditions and you want to check different variables in each case.