This course has been updated! We now recommend you take the Basics of Go course.

Check out a free preview of the full Go for JavaScript Developers course:
The "Control Structures: If & Else" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna demonstrates how blocks are written in Go, how to handle an error returned from a function, and a common way to handle errors using an if block.

Get Unlimited Access Now

Transcript from the "Control Structures: If & Else" Lesson

>> And the next thing I'd like to talk about is control structures in Go. And we're talking about if statements, four loops, and switch statements. Some of the kind of more common ways to navigate through logic in our applications. So I'm gonna be living in the ifs.go file which is in the third section here in basic_syntax/code/ifs.go.

[00:00:22] So I'll hop back over to our code editor here. So I'd like to start with a comment about some of the code snippets you'll find as we work through the rest of the workshop. You'll notice that when I first walked into this file, there was a lot of commented out green text.

[00:00:41] At the top of the file, there's a little instruction here to uncomment this entire file, and this will kind of let us walk through some of the code blocks in smaller chunks. So for the first if block, I wanna talk about, we have a variable declared as some bar and it's set to an integer of 9.

[00:00:57] In my if block, you'll notice that the first statement is a conditional, but it doesn't have parentheses which are not required in Go. Then you have our opening curly brace, and then whatever logic we'd like to execute lives within the curly braces. So the curly braces are required, parentheses are not.

[00:01:21] If you wanna get a little bit more specific, Go does come with else if and else block, so which behave is similar to JavaScript. Whatever is executed within the first if block that returns true, so if some bar is greater than 100, we'll get that first block, and it will not execute the following else if or else blocks.

[00:01:42] So the next thing I'd like to talk about a little bit is this assignment operator with the shorthand syntax, so we have err assigned to some function. And this will be a pattern that we see pretty frequently throughout the day, and err is gonna represent an error here.

[00:01:58] We're gonna talk about error handling pretty in depth down the road. But right now, we're assigning a variable which is gonna an error to some function. So if this function returns an error, save it to this variable so we can use it and figure out what we wanna do with it.

[00:02:11] In our if block here, We're asking if that error is not nil, meaning if something was stored in this variable, we want to execute some code to handle that said path. And so for error, we're simply using the err.Error function which we'll talk about later. But assuming error is not mil, something horrible happened, what we wanna do about it?

[00:02:37] This is a very common use of an if block in Go to handle errors, and we'll see this a 100 times today. And the last pattern, I'd like to discuss it will start using pretty shortly, is one of my favorite patterns and go. And it allows you to both define a variable and then check about that variable in a conditional sense before executing code within curly braces.

[00:02:58] So what's happening on line 37 here, is we're saying, hey, set err to the result of someFunction once it executes, and then check to see if that thing is nil. So we're both defining the variable, we're checking the conditional, and then we're firing off whatever code lives within the curly braces within one line.

[00:03:17] The other interesting thing about this pattern is that anything that's scoped within an if block is kind of contained within that if block. So now, this err variable, it's not accessible elsewhere. If we wanted to redefine it, we could. It's kind of like scoped within this little chunk of code.

[00:03:32] So we can reuse it as much as we want throughout our program which is gonna be something we do in a little while.