Control structures

GetGoing: Introduction to Golang Data types and control structure
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone in the last video we talked about interfaces In this video we're going to be talking about control statements. So I'm gonna spin up a command and I'm going to say hello world so yeah and then after this hello world I'm going to explain to you His word go has to in terms of control statements go as if else conditions go has for loop and surprisingly go does not have a while loop. So but the for loop and go is very powerful. And then you're gonna see that in just a short while and then we have switch case statements and we have keywords like braid continue etc, etc, etc. So, diving into if else conditions. So, go guidelines read that if the syntax or the rather the format should be like this.

So you have an if condition and then you have is if and You know? So one thing to note as notice that the brackets here are very necessary. So we declare a flag flag to true. And then I can say if flag. Then as if flag is equal to, or Rado. Let's ignore flag.

Let me introduce the concept of pointers over here. And I can see that f is equal to true and flag is equal to the address of f. So you're going to see a lot of values, a lot of values like this. So what happens during web dev is or rather concurrent, concurrent Development also that we prefer to have everything in the terms of pointers. So, you might see a lot of Boolean values also being declared declared as pointers, the benefit is that we get access to this nil block. So we can say that Okay, the first if condition should always be flag is equal to is equal to, just so we know that the memory is the memory even being allocated to it or not. And then we can say, Is it true?

As you can see, and false, false kind of a grid control string, you can say FMT dot print line. value is so you do a quick good to go. And then you get a Hello World. Yep. So going to mean five again, Now, this is how effects conditions work if else conditions also support these chaining up of events standing up of conditions rather, and an order. So I can say two is equal to is equal to three, in this case, this is always going to be true.

Or I can say or five, not equal to six, which is always going to be true. Two equal equal to three is going to be false. So and one thing that you might solve two things that you might have seen were that okay, you need me to include these records. If you don't include these records, it's gonna return an error. So you do need these brackets even if your line is just one line. So in other programming languages, like C, c++, Python, etc.

You don't need to say that, okay, you don't need to declare brackets basically. And then one more thing is that with languages like C and c++, your if blocks were always like if, and then you get a pair of brackets and inside the bracket to type whatever things whatever control statements are. One today, and even vitam three, also you can do this and in JavaScript also. So, but in golang, you don't need brackets anymore. And in fact, if you do, if you do type something using brackets, then it's automatically going to throw an error. See, it automate automatically remove the brackets for so now moving on to for loops.

So for the for the syntax is quite similar to the one you had in C and c++. So you can say for i is equal to zero, i less than 10. i plus plus. And then from now if you run this you get one tonight. Cool. So this so what you can do if you want to run out in front of you You can simply remove everything and then to do anything you want. And it's gonna run an infinite loop and so it's gonna run till you press Ctrl C at your terminal.

So this is the syntax of running an infinite loop and then you can also iterate in it as you can see that Okay, let me define an area for so like my ID is an ID of type string, which is, which is basically an audio file names. My son name and then my middle name, which doesn't exist. Okay, as far as I know. So I can say for i is equal to range over it, and then print everything that you get. So I do a quick good, Lord, go and then I ordered one, zero And that is because it takes two values. So the first value is an index index, the second value is actually the value.

So you have the index and then you have the value. value. So if you look at this closely, you can see it will zero, the zero index element is longer. The first element is Sharma. And the second element is building, which is correct. And then you can also iterate over maps.

So let me make a new map. My map is equal to map of string to an interface. So you'll see me a lot of times making maps with the key string and the values interfaces because really, if you're doing dynamic, if you're doing any kind of dynamic programming, where you don't know what your input is going to be, it's safe to play it safe. It's better to play it safe. And then my name is equal to H. Now do loop over map, use a for key and value in range of my map. Again, the syntax is pretty simple.

Mikey is forcing this. Mikey is both and guess what about new is also disposer v. Suppose and v is something that you want to use for structures interfaces or not. So I'm quickly going to do a printf instead of print line over here, because I want a formatted string as an output unless a key and value just like in C, no expected expression, line 27 Yeah, so make working on it See, I look like good on. And then they look you Lord, give the H value is grantee and then in another iteration you got key name and the value. So here you are easily able to iterate over a map. So in the next video I'm going to be showing you about switch conditions and how you can play around with those.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.