Check out a free preview of the full Go for JavaScript Developers course:
The "Control Structures Solution" 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 live codes the solution to the exercise.

Get Unlimited Access Now

Transcript from the "Control Structures Solution" Lesson

>> So let's work through exercise 3a together. First part of the directions is to create another exercise 3a.go file. And then in the main function, we wanna declare a variable that has the value of a sentence. So I'm going to go ahead and set up Our go file template which starts with our package main.

[00:00:34] And now we're gonna be printing some stuff to the console. So we'll go ahead and import our fmt package. And then in func main, the first thing we wanted to do was instantiate a variable of a sentence. So I'm gonna call it my sentence you shorthand syntax. And use an incredibly enthralling sentence of, Hello, this is a sentence.

[00:00:54] Next steps in our instructions are to iterate over that sentence using a range. So to do that, we're going to start with our fore loop. And so the two arguments that we have using range is we have that index, and then we have a value of each thing we're iterating over.

[00:01:11] And that is assigned to a value of our keyword range, and then the collection we're iterating over, which in this case is mySentence. And then our opening and closing curly braces to scope that fore loop and the code within it. So step four here, if the index of that letter is an odd number, print that letter.

[00:01:34] So the first step is, I'm gonna add an if block here. And so I wanna check to see if index module two equals 0, using the two equal signs to indicate equality. And if that returns true, I want to print the value. I'll bring my terminal backup. And if I go run section 3 basic syntax, code, and then exercise 3a.go, you'll see that I'm not getting letters once again, I'm getting that byte value.

[00:02:11] So I'm going to put back in my string function to convert our type to a letter, save the file. So when I run that file, I'm only seeing the letters if that index value is an odd number. So in this case, I am using the index variable here on line 9.

[00:02:35] So I'm setting index modulo 2 equal 0. But if we were to kill this chunk of code and just go back to kind of our, Print line of the string value. If I save this and I run my code again, I'm gonna see an error message and this error message is saying the index is declared and not used.

[00:02:58] And because we have two values on the left hand side and go is following it's rules and saying, once you declare it you have to use it. But in this case I don't wanna use index. I'm not using it in any productive capacity and so go allows you to put an underscore into any variable that you're not gonna use within the code itself.

[00:03:14] And it's gonna ignore it and allow you to still compile your code. If I run it again without underscore instead of index, it doesn't yell at me, I can print out that value. And that's something we'll see pretty frequently as we continue as well.