- Overview
- Transcript
2.5 Conditionals
Often in our code, we need to make decisions about what to do based on input data. The language features that let us do so are called “conditionals”. Go provides several conditionals, and in this lesson I’ll discuss two of the more common: if
and switch
.
Related Links
1.Introduction3 lessons, 12:09
1.1Introduction01:01
1.2Prerequisites03:08
1.3Installing Go08:00
2.the Basics8 lessons, 1:15:15
2.1Variables and Types11:28
2.2Constants and Comments04:44
2.3Functions13:27
2.4Loops06:41
2.5Conditionals12:34
2.6Pointers09:40
2.7Structs08:28
2.8Arrays08:13
3.Building Web Servers in Go4 lessons, 43:38
3.1Building a Basic Web Server10:44
3.2Serving HTML to the Browser12:27
3.3Creating Templates10:11
3.4Dynamic Templates10:16
4.Conclusion1 lesson, 01:32
4.1Conclusion01:32
2.5 Conditionals
One of the most important things to learn about any programming language is how to deal with conditionals. Meaning if I wanted to check whether or not a number is between another value and another value. Or if a string is of a certain length or things of that nature. I need to be able to incorporate that type of logic into the world of my application, and we typically do that with, as I said before, conditionals. Which in the world of the Go programming language typically take the form of if statements or the whole if else, if else statements and switch statements. So, let's take a look at how we can handle this in Go. So let's start with a very simple example, and we'll just call this our basic example. And what we wanna do is we wanna create a function and this is gonna be called lessThan10. I think you can probably see where I'm going with this, but I'm going to create an argument list here and it's gonna take in one value. This is gonna be a number and this is gonna be an integer and we're going to return of Boolean. So what we wanna do in this case is we want to test the input. We wanna test our argument to see if that is truly a number that is less than 10. So how do we do that? Well, we use the if conditional and in Go we once again just like in the for loops or any of the looping constructs we don't use the open and close parentheses for the body for that conditional statement of our ifs. All I need to do is I need to say is if num is less than 10, and then we use our curly brackets to denote the body of our if statement. So if this conditional is true number is less than 10, then I want to return true. Pretty simple, and then just like in many other languages I could then say else. And I could say else and do some sort of logic in here I could say else return false. So if it's less than 10 we return true, if it's anything other than that meaning if it is equal to 10 or greater, it's going to return false. Now this is absolutely valid syntax this will work, but this is a little bit extra that we don't need to do. So when we're going through the process of creating this if statement, let's go through the logic a little bit slower. If the number is less than 10, I want to return true. Since we're only dealing with two options here or we've eliminated all the possibilities here. By saying if it's less than 10 I know if it falls into that bucket I wanna return true. Anything else that happens I don't really care I just want to return false. So that really means that this else block here is unnecessary. So I don't have to write it this way because if the number is less than 10, we're going to exit the functionality, we're going to exit this function and it's going to return true. So instead of wasting time and space with that else, we could simply say at this point return false. Because if it's less than 10 it will exit the flow of this function here and return true. And then if it's not less than 10, or if it's equal to or greater than 10, it will just jump out of here and return false. So that's a little bit more of a a concise way to write an if statement in pretty much any language out there but in Go as well. So then I could come down here and I could simply call my lessThan10 and I could give it a number. So let's give it 9. So if we save this and say go run Conditionals.go, I'm going to get true. And if I were to say this was 10, save that and then rerun, I'm going to get false. So as you can see here this is a very simple example of how to write an if statement in Go. Now we can get a little bit more verbose or a little bit more detailed with our scenarios it might not always be as cut and dried to be a true or a false so one or the other. We could have multiple options, and when it comes to having multiple options in comes the world of the if else statement. So let's create another function here and this is gonna be an example of if, else if, and then ultimately else. But once again you can leave that part out if you so choose which I would definitely highly recommend. So we're going to create a function and this is going to be greet, and we're going to be passing in a time. And for simplicity's sake, we're simply going to be passing in time as an integer, meaning the hour of the day. So this is going to be an int and then we're going to return a string. So what we wanna do here is let's assume that this is going to be a 24 hour clock. So it's gonna be numbers from 0 up to 23 representing all the 24 hours of the day zero inclusive. And I want to be able to output a greeting or at least return a greeting so that I can output that greeting based on the time of day. So let's think about a couple things that are going on here. So I could present a value into my argument here into time that is valid. So it could be anywhere from 0 to 23, and everything should work fine. But what happens if I pass in something outside of the accepted range? I need to be able to handle that too, so let's start with that case. So I'm gonna say if time is less than 0, because obviously that's not a valid time, there's no negative time values. But then I also want to handle the case where somebody is passing in 24 or greater since that's not a valid time either. Because once we get beyond 23 and we're about to turn to 24 we go back down to 0 because it's just the rotational clock concept. So I can do that in separate statements as I started to do above where I could have an if and then I could do an else if, which I'll show you in a second. But what I could do also here is I could combine multiple conditionals, and put them together with a logical operator. And in this case, that's what I'm gonna do. I'm gonna include the logical or, which is represented by two bars. So I could say if time is less than 0 or time is greater than 23 this is an invalid case and I'm just gonna return something like like this is confusing I don't understand why you're doing this. Now we can start to handle the valid cases. So in this case we wanna do an else if because we only want this to happen if this particular condition is met. So we'll say else if, and in this case if time is less than 12 meaning it's pretty much morning time, we're going to return, as you might imagine good morning. And then we can say another one else if time and so if we've handled the bad cases, less than 0, greater than 23 and less than 12 now. Now I wanna say well if the time is greater than 12, and so that now we're talking about in a range here. And I can do and with double ampersand just like I can with other languages. Time is less than seventeen. Now this is basically the afternoon. So this is anywhere from noon until five o'clock PM. So I can say good afternoon and then the other option would be for evening. So I could as I mentioned before put an else in here and then say return Good evening. But since once again I am in the situation where I've handled all of the cases here and in all those cases I'm jumping out of my flow to return a value. I can simply drop down here and say return, Good evening. Something like that. So you could obviously add in other ranges there, or tweak them to your liking, but that's the basic concept. So now if I were to come down here and run my little sample here. I could say greet, and I want my greet, so let's start with a negative 1. Let's start with a bad case here. And I'll run my application and I get which is what I would expect. Now if I were to put something early in the morning, or actually 24, we'll do late at night, an invalid one again. So we get again. And now I can say if I do 10, I would expect this to be in the morning. So we'll run that, and I get Good morning. And then I can do a 14 which I would assume to be an afternoon time, which I do get. And then anything out above and beyond 17, so we'll say 20, we'll save that. And we'll run it, and we get, Good evening. So there you have the multiple kind of branching mechanism to be able to handle a lot of different conditionals with if and else if. Now finally, there is one other main mechanism to be able to do conditionals outside of an if. And that's going to be the switch statement. Now a switch statement, once again, is very similar to switch statements in other languages, but let's just kinda go over some of the syntax here. So I'm gonna create a function here and I'm gonna call this platform, just like that. And I am not gonna take any inputs, but I am going to return a string. And this is kind of a cool mechanism that's going to demonstrate how Go is aware of it's surroundings and can kind of look at what it's doing and what sort of platform it's running on. But in order to do this, I'm gonna need to import another library here and that's going to be the runtime library. So this gives us access to some of the things that are going on while our application is running. So, let's see how we can use them. So, what I want to do is I want to check to see what platform we're running on, or operating system that we're running on and then I wanna return something. So let's do this in a switch statements so I can say switch. And in my switch statement, I can actually set this value called OS. And I'm gonna set it equal to runtime.GOOS. So this is going to be the operating system that we're currently running on. And then I want to switch on that OS value and now I can set up multiple cases within my switch statement just like I would in other languages. So I could say things something like case in my case this is going to be darwin is the representation and we're going to say that we're going to return OS X in this case. And then I can do another case and I'll call this Linux, which is going to be if you're running on Linux we can do a return Linux. And then we can continue to put things in here, but one of the more common mechanisms to kind of stop or be like that bucket to catch everything else in the world of a switch statement is the default, and we have that here as well. So I can set a default and say whatever else is out there running whether it's Windows or something else it's going to fall into this bucket. And then in this case all I really wanna do is, and actually instead of, and actually to make this a little bit simpler I'm not going to return anything I'm simply going to print these out. So I'll say format.Println and we're going to print OS X in this case. And then we'll do a similar thing down here Println and I'm going to do this because I can show you another trick for printing information out to the screen. And in my default here I can now use a different print function and it's going to be Printf. And as you can see here I'm going to be able to use a formatted string. So what I can do here is I can give my formatted string and I can use little variables or markers within my string to replace once I get the data. And I can do this by using the percent sign and s. So this is going to say print whatever string out that I have and I want to print out os. So now that I've done that, I can save this, and it's complaining at me because I used the wrong delimiter here. I need to use a semi-colon here. Save. So I can separate my declaration and initialization of my variable here before I actually switch on it. So once I've done this I can come down and I can run my platform function like that, save. And if I were to run this now you're gonna see that I forgot I've changed what I'm doing here. So instead of returning a value I'm just printing it out so I can now save this. And let's go ahead and rerun and you can see that I'm running on OS X. So there you have it there's some mechanisms to handle conditional statements in Go using both if, else if, and else as well as the switch statement.