The Golang Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome back to the channel in this video what we're going to do is cover the fundamentals of go so if you want to start learning go and start being comfortable coding in the golang programming language this is the video for you golang is a wonderful language and i'm excited to present this course for you now let's just go ahead and get right into it now the first thing that you're going to need is you're going to need to have go installed on your machine in order to do this just go to go.dev and very simply go to the download section and then download whatever go version you need for your operating system i'm on mac so this is the one that worked for me if you're on windows this one will work for you if you're on linux then this one will work for you this is a very simple installation you just follow a simple wizard and then that is pretty much it so i'm not going to go ahead and do it because i already did it now once you install it to double check that you have it installed you can open up your terminal or your command prompt if you're on windows and you can very simply say go if you get all this right over here that means yes you have go successfully installed now when it comes to go we're going to be learning quite a bit and we're going to be coding quite a bit now the text editor that i am going to be uh using is visual studio code so these are all the different things that we are going to learn about and we're actually going to go ahead and code them out inside of visual studio code i love vs code because it comes with this amazing go extension so if i go to if i zoom out a little bit let's go here zoom out a little zoom out there we go we should see the extensions tab right here and we can go ahead and download the go lank extension which i already have and it gives you nice syntax highlighting it also comes with an integrated terminal which will allow us to run our go files so i highly suggest that you use visual studio code this is a great library to utilize now these are all the different things that we are going to be learning about to understand the fundamentals of go in future in the future i'll make more advanced lectures for more advanced go concepts but these are just the fundamentals and some of these are pretty tough to grasp now if you ever want to see this code do not worry i'm going to go ahead and host it on github so you can always refer to it and i'll have the link in the description below so that is that and i hope you guys enjoy the course and well enjoy it now that we got everything set up let's go ahead and start learning some go code now the first thing that we are going to learn about are variables now variables are ways where we can store our data and that data can be variable now if you don't understand this concept all good let's just go ahead and dive right into it so i went ahead and i opened up vs code i created a file you can call this anything that you want but i called it one dash variables dot go it needs to have the dot go extension for it to be a go file and then in here what we're going to do is we're going to say package main and do not worry if you don't understand what this is i'll explain it a little bit later and then we're going to do import ftm and then over here we're gonna say funk main and then we're gonna do so like that so this is just a function inside of go which i'll talk about in future lectures now essentially what's going on here is when we run this file what is gonna happen is it's going to run everything inside of this main block so this is essentially where we want to run our code now this right over here is going to allow us to print some things to the console so let me go ahead and demo this so i'm going to say ftm and then over here we're going to say dot print ln so this is print new line and then in here i'm going to say hello my friends so now what i can do is i can open up this new integrated terminal so go ahead and do that and i'm going to say go run and then i'm going to specify the name of the file so this is going to be one dash variables dot go so i'm gonna go ahead and run that you can see here i get hello my friend so again anything inside of this main function that is what is going to be executed and then ftm is just it's going to allow us to print some things to the console it also has a bunch of other methods but we're mainly going to use it just for printing okay so now let's go ahead and talk about variables so let's say i want to store my favorite book in this code well how am i going to do that well i am going to do that with variables i'm going to say var so this is a keyword var and then i'm going to say fave book so favorite book now you can notice that this is uh so the first letter is lowercase and then the next letter is capital case so this is the common convention for creating variables inside of golang this is known as camel case so the first letter is always lowercase every other letter is capital case so if i were to say var uh m i cool you can see here the first letter is uh lowercase the next two are capital case again camel case so i'm going to say var favorite book fave book is equal to and let's just say harry potter that is my favorite book so that's essentially how we go ahead and create variables now i can access this variable so let me go ahead and actually print this out so i'm going to say ftm dot print new line and i'm gonna say fave book so let's see what this is gonna get me well it should get me harry potter because that is exactly what it is storing so if i go ahead and run this as you can see we have harry potter okay so this is looking really really good okay so what is the next thing well let's say i you know what this isn't my favorite book anymore well i can change it i can always change the value of this variable i can say now favorite book is equal to let's say power of habits that's actually one of my one of my favorite books this one harry potter actually never read so now let me go ahead and print it here go ahead and run this code and you can see we have harry potter and then we have power of habits so what are we doing here well the first thing that we're doing here is we're declaring the variable so we are declaring the variable and then we are assigning it to this value so declaring and assigning so we're assigning it to harry potter now over here the variable is already declared what we can do is we don't have to specify the var again the variable is already declared we are just reassigning that's exactly what we're doing we're just re-assigning the value now what if i try to assign the value or reassign the value to something goofy like for example let's say i try to assign it to favebook is equal to 12. well you can see that go is actually giving us some errors so you can see here cannot use 12 untyped integer constant as string value in assignment so what this means is that well the type of this variable is a string why is it a string because when we first assigned it the value that we assigned it with was the string harry potter and so this variable should always be a string and any time we try to assign it to something that is not a string for example an integer a number well it is going to yell at us so again this is something that we can't do and this is something that is very important to understand go is a typed language so it's not you know you can't do stuff like this it's not loosely typed it's strongly typed so i'm going to go ahead and comment this out all right so now let us move on now because go is a strongly typed language what we can do here is when we declare a variable we can also specify the type of this so let's say i want to specify my other favorite book and over here i'm going to say bad blood so that's my other favorite book and over here i can also specify and by the way it's yelling at me just because it's not being used so if i go ahead here and i print it out it should stop yelling at me so let's go here there's no error so over here right after the variable what i can do is actually specify the type of this so i can specify that this is going to be a string now if for some reason i decide to specify that it's going to be an integer but then i assign it to a string you can see that it is yelling at me now you might be thinking well why in the world do we want to do this because as you can see here go is able to infer the type without us specifying it well in this case there's probably no real use case of uh of uh defining the string if we are declaring the variable as well as assigning it to a value this probably isn't the best idea it's probably best practice just to let go infer it however there are times when we very simply just want to declare the variable but we do not want to assign it so for example we can do here let's just go my third favorite book i guess these they're not really my favorite books if i have so many favorites so i can say var third favorite book and that's it that's all i i can do now over here i can say third favorite book and then i can assign it to something else now what i don't want to happen is for me to be able to assign it to like a number that's not what i want to happen i'm not sure why this is yelling at me here i'm missing variable type oh yeah so that's exactly why it's yelling at me is is i do not want to go ahead and assign it to something that i don't want it to be like an integer or a boolean value or whatever so over here we all have to specify the type in this case because we're not assigning it to a value so in this case i'm going to say string and then over here when we go ahead and try to assign it to something well it's going to yell at us so now i can go ahead and change it to whatever i want a string whatever doesn't really matter let me change it to a real book say diary of wimpy kid so diary of wimpy kid nice all right and again it's yelling at me just because it's it's not being used now there's many different types inside of go so for example there is a number type so i can say integer now there's different types of integers so we have integers we have floats which are decimals we have boolean values we have a bunch of different things i'm going to go ahead and say integer for this one because it is just the simplest thing and i can also say var m i cool and i can specify that this is going to be a boolean value now over here you can see that over here i'm going ahead and assigning it this third favorite book variable i'm assigning it to this string over here but the other ones i'm actually not and if you don't assign it to anything it's actually given default values so for my age which is an integer it's going to be given the default value of zero for am i cool boolean if i don't assign it to something it's going to give me the default value of false to prove this to you let's go ahead here and let's just go ahead and print all these things so i'm going to print this and by the way a string if we don't provide a value for it it's going to give us the default value of just an empty string so let's go here let's just copy all that stuff and now let's go ahead and run my code and so you can see here diary of wimpy kid zero false so i'm not i'm not very cool unfortunately okay so this is amazing so now what i want to move on to is uh compound creation so sometimes uh you know i want to create multiple variables and i can most definitely do them like this but sometimes you know this this is just way too long i just want to create multiple variables very very quickly and we can actually do that in one line so we can say here var and then i can say var my let's say my favorite number and let's say i also want to store not only my favorite number i also want to store my favorite chocolate as well as my favorite team now again i can always do something like this var and then favorite chocolate and i can say kitkat and then over here i can say var uh favorite team this is like an nba team i'm going to say the knicks for this i can always do this this this is completely valid but i can also do something like this i'm going to comment this out because i'm going to use the same things here i can say var and then i can say favorite number and then comma favorites chocolate and then comma favorites team and i can say that that is going to be equal to 27 is my favorite number and then kit kat is my favorite uh uh chocolate and then lastly the nyx are my favorite team so this is also completely valid and this is known as compound creation so we're creating multiple variables uh uh in a compound fashion i guess all right let's go ahead and actually write that down i'm going to say compound compound creation okay amazing now another way that we can do this so we can do this actually multiple different ways you can also do block creation so if we're not really a fan of this syntax let's go here i'm going to say block creation so if we're not a fan of the syntax i'm going to go ahead and copy this comment it out copy comment it out and then i'm going to paste it so what we can do here is right after the var we can actually create a we can have two parentheses and then enter and then this right here is going to be our block so i'm going to say my favorite number is equal to 27 and then i can say my favorite chocolate is equal to the kitkat kit cat and lastly i can say my favorites team is equal to the knicks so this is if i prefer to just have it you know in a block rather than just a a line like so uh and yeah that's pretty much it now this is somewhat similar to this but i don't have to specify the var keyword every single time now again it's yelling at me because i am not using the values so let's just go ahead and print them out so i don't have that yelling at me anymore so i'm going to say print new line no no i do not want to do that and let's just go ahead and print them let's just see that they actually end up working and over here you can see seven kitkat nyx okay so this is looking a-okay all right so now let's actually move on let's say you know i'm i'm always well i'm not always but let's say i am very frequently assigning and uh well declaring and assigning variables um there's actually a shortcut to do this so if you want to declare and assign a variable and not just declare a variable instead of using the var keyword what you can do here is let's say favorite animal now i want to get my favorite animal you typically let's say my favorite animal is a tiger i would just say var tiger by var favorite animal is equal to tiger this is typically what i would do but there is a shortcut for this because i am declaring as well as assigning i can just remove this var keyword now this isn't going to work like so what we need to do is right before the equal sign add a colon so over here we can say var animal colon equals tiger and this is completely equivalent to using var again just to prove to you that it works let's just do a print i'm going to print in this variable tiger okay so this is amazing now uh of course we can actually do kind of the the same thing in terms of uh in terms of the compound creation with this declaration as well so what i can do here is i can say for example let's say i have multiple pets i have pet one and i can say here pet two pet three and then i can say colon is equal to and then over here i can say cat and then i can do comma dog and then i can say comma rat like so and if i go ahead and let me just print these pets this works a-okay so let's go ahead and just print all this you should see cat dog rat now in terms of uh uh so this right here again is a declaration and assignment now one thing that we cannot do is something like this pet2 and then declare and then assign is equal to parrot this this is not gonna work we can't reassign by doing something like so that works however if we are going to use uh this kind of syntax where we have the commas this actually is a okay so we can declare and assign something that already exists so for example let's say i want to declare and assign parent pet 4 but i also want to reassign pet 3 by using the comma i can actually do that so i can say pet three and then over here i can say um i don't know whale a whale is my pet so let's go here and now what i'm gonna do is just print out all of the pets print them out now you can see here that it got reassigned so so it's we can't actually do this syntax if we're using the commas all right and that is pretty much all it is that we need to know about um variables there is one more variable i do want to talk about and those are constants so right here when we define variables typically we either use this syntax or we use var but another thing that we can actually use is const so let's just go here and say constants so we can use const and i can say my name so let's say my name is always going to be my name that is a constant it's not going to change so if this is something that is not going to change we can actually add const here i'm going to say lathe and then if i try to let's say my name is equal to [Music] sarah you can see here that it yells at me because well this is a constant i cannot change it so again if there's something that variable that's not going to ever change values it's best to use a constant all right so that pretty much sums up variables i hope that is nice and clear and now we're going to move on to the next section welcome everybody to a brand new section in this section we are going to talk about operators now instead of explaining exactly what an operator is let's just go ahead and dive into go code and i'm sure you guys will understand it once we see some examples so i went ahead and created this operator's file so o2 dash operators dot go i'm going to go ahead and just copy the package main import fmn fmt and then the function because we're going to need that and let's just go ahead and create it like so now so far we have learned how to declare variables so for example let's say i want to declare just a random number let's say num1 and i can say that this is equal to four so i'm declaring and assigning now can also do num2 is equal to three now what if what i want to do is i want to have another variable that is the sum of both of these values so as you can imagine i can do var sum and one possibility is for me to just manually add these two up and say that okay well this is gonna be seven however obviously this isn't a great idea because the value of these variables could be well variable we do not know exactly what they are so and another option is just to very simply add up num1 plus num2 and this is where we are going to start using operators now specifically there's different types of operators but for this we're going to use arithmetic operators so arithmetic opirators and the arithmetic operators that exist are the plus sign we also have the minus sign we have this dash which is the divide we have the stars we also have uh the um this uh percentage that i'll talk about a little bit later but over here what we can very simply do now is just say num1 plus num2 and that is going to give us the sum and it's going to assign that very assign that value to this variable over here all right let's go ahead and just quickly test this out let's just say print new line i'm going to say sum let's go ahead and open up our integrated terminal and i'm going to go ahead and run this time a new file so o2 operators dot go and as you can see here we get seven awesome okay now of course as i said we have multiple operators so if we want let's say the difference so the difference which is just the uh well the difference between the two numbers it's going to be a num1 minus num2 and if i were to print out the difference i will get the difference let's just go ahead and do all the operators and we'll print them out now if i want to divide two operators and i want to get the quotient so we can say quotient that's going to be num1 divided by num2 and if i want to get the product which is multiplication i can say var product is equal to num1 and then over here the star is times num2 and let me just go ahead and print this and i'll show you exactly what this uh percentage sign does so let's go over here and let's go here and let's go ahead and run this code and now you can see we get seven which is the sum one which is the the difference now over here you might be thinking why in the world are we getting a 1 well that's because this right here is of type integer and it's not of type float so that's why we're not getting any decimal points so the the reason why over here is if we wanted to do that you would have to assign this to a particular float number so i can say here float and now if i were to divide it you can see we actually get um decimals like so all right and then the product is going to be 12. now let's talk about this percentage sign this is just allowing us to get the remainder so over here let's just say remainder i'm going to say that this is going to be equal to and i'm going to say num1 and then i'm going to say percent sign and then 2. so num1 percent sign two and i believe the reason why this is not working is that this has to be an integer over here so invalid operator so let's just go ahead and remove this for now just keep it as an integer and what this is going to give us is the remainder so num1 is uh so num1 is 4 which is an even number so how many times can we get a two into four well we can get it two times and the remainder is going to be zero so if i were to go ahead and run this you can see we get zero and now if i were to use num2 well i can only fit one two in there and the remainder we're gonna have a remainder which is one so over here we should have a value of one now if i decide for some reason to put like 77 well we can fit no 77 values into three and the remainder is going to be three at this case so let's go here as you can see three so that's pretty much all it is for uh arithmetic operators are not very complicated so now let's actually move on to relational operators so let's go here i'm going to say relational operators so let's say operators so let's say what we want to do and we want to see which number is bigger uh right now obviously we can clearly see that number one is bigger but maybe we don't have the values or maybe it's not that transparent so it could be you know we could assign this to something else so it could be like num1 is equal to number of uh products and then over here num2 could be like number of whatever and we want to see which one is bigger right because we obviously we can't see it's like uh because we don't see the value at this point so what we can do here is we can use relational operators so let's go here and let's just say var result var result and let's say i want to see if num1 is bigger than num2 now to do this well as you can imagine we're going to use this relational operator right over here and what this expression over here is going to eventually yield is a boolean value so a boolean value is either going to be true or false so if num1 is greater than num2 then the value is going to be true else it's going to be false so let's go here and what i'm going to do is i'm going to do a print so prints new line let's go ahead and print that result and again this is going to yield a boolean value as you can see true another thing that we can do is well let's see let's actually go ahead and change num2 to 4 and let's just think about what this is going to give us so let's go ahead here and this is going to give us false because what's happening here is well no num2 is not greater or num1 is not greater than num2 because well this is 4 and then this is 4 as well let's say we only want to check if it is greater than or equal to well in this case i can just say greater than and then equal to like so and this should yield us uh true let's go ahead and move this back to three let's say i want to see if if num1 is less than num2 over here i can just say this expression as you can see or i can also add less than or equal to like that i'm not going to go ahead and run it because you probably get this now another relational operator is i want to check if num1 is strictly equal to num2 not greater than not less than just equal to well for this i can use two equal signs over here we use one equal sign for of course assigning a value to a variable two equal sign is a relational operator just to check if two values are exactly the same you can also check if they're not equal which in this case is true if i go ahead and run this we should get true because num1 is 4 this one is 3. so those are relational operators very very simple stuff let's go ahead and move on to the last set of operators which is logical operators so i'm going to go ahead here just say logical op array tours and now logical operators are way that we can combine different expressions so over here this is an expression right here so let's say what i want to do is i want to check for example let's go ahead and create two new variables here i'm going to say const name and i'm going to say that my the name is lathe and var age i'm going to say 25 something like that so let's say what i want is i want to check if i want to check if you know you're allowed to get invited to my party so let's go ahead here i'm gonna say var invite to party now let's say that i am very strict with the people that i can invite to my party i only want people that have the same name as me and are older than 23. so i just want to have a bunch of lathes in the party and the we cannot have any 10 year old lace we want to make sure they're all older than 23. how do we do this well as you can see here we have two different expressions that we have to create so let's just go ahead and create them right here so the first expression is well the name has to equal length now the second expression is well the age has to be greater than 23. so somehow what we need to do is we need to combine these together and we need to check that both of these are true so if one of these is false we just want the overall in invite to party value to be false as well well we can do that with a logical operator specifically we can do that with the and logical operator because what we want is we want this to be true and we want this to be true so over here i'm going to say that that is going to be equal to name is equal to lathe and then the and operator is just very simply two ends like so and then i can say age is greater than 23. so let's just go ahead and give this a quick test by doing a quick little print here so invite to party maybe you can call us invited to party this is a boolean value and you can see here that this is returning true why well because the name is equal to lathe and the age is 25. if i were to change this to um i don't know let's say lawton well this is going to return false because this expression right over here is going to return false this one's going to return true because the age is 25 but because we have the end we need both of them to be true so let's go ahead and run this you can see now we get false all right this is looking pretty good so that is the and operator let's say that this is just way too restrictive you know i don't you know it just turns out that i'm the only person in the party uh it's just not great so what i want is okay well of course i'm gonna be invited to the party so name is equal to lathe so your name could either be lathe or if your name is not lathe then well i want your age to be larger than 23. so to do this what i can actually do here is use the or operator so again what this means is that well if your name is equal to lathe this is going to return true and let's say your age is not greater than 23 with the or operator we just want one of these to be true so if this is true it's going to kind of override this and the overall expression is going to return true now let's just go ahead and prove this to you by saying 12 and then over here see it is returning true now another thing that i can do is just change the name and make this older than 23 and that should also return true all right looking good okay so that is the or and the last one that i want to talk about is the not operator so let's say i want to absolutely ensure that i am not invited in to my own party because i am very boring so what i can do here is i can make sure that the age is greater than 23 so i can add an and here and then over here i can actually just wrap this in parentheses and say not so this expression overall what it's going to do it's going to return true because lathe is equal to lathe but then with this exclamation point i'm going to revert the boolean value from true to false now if this right here was actually false it's going to revert it from false to true so over here i'm just simply saying your name is not equal to lathe and your age is greater than 23. so this actually is going to return if i were to i didn't save it sorry if i were to save it you can see it returns false if i change my name now you can see that now returns true yeah that's that's pretty good and we can actually do a lot of kind of complicated things we can really combine these things together so one thing that i can do here is let's say what i want is okay i want if your name is equal to lathe you're not invited to the party and you have to be between the age of 21 and 90. we don't want anybody over 91 years old at the party that's just way too old so what we can actually do here is say and and then over here we can have parentheses and then we can have another expression in here so they can say age is let's say greater than greater than or equal to 21 and then and and then age is less than 90. so what's going to happen here is because we wrap this in parentheses this is going to resolve this expression right over here and in this case well the age is greater than 25 or is greater than 21 and it's less than 90. so overall this is going to return true and the name is well in this case the name is lathe so this one's going to return false so overall the expression is going to return false so let's just go here you can say false now if i were to change this to something else uh you can see that it returns true now if my age is let's say 16 if i change the age to 16 you can see that this is going to return false because overall this expression is going to return false so those are operators so i hope that uh makes sense and i hope that was a fun lecture all right my good friends let's go ahead and move on to if else statements and how we can utilize this and go so just as a quick reminder at the very bottom over here we had a bunch of conditions just to either yield as true or false and assign it to this variable invited to party however maybe if you are invited to the party we want to do some action maybe print something to the console over here so how could we do that because again right here all this does is just return a true or false value well this is where we use if else statements to just check if a statement is true i want to perform some action else i want to perform something else let's go ahead and see this in action i'm gonna go ahead and create another go file and as per usual let's go ahead and copy these three lines close this off close this line right over here and in here we're all going to write our code so what do we want well let's go ahead and create a variable and i'm going to say this variable is going to be age so i'm going to say age let's say 34. now what i want to do is if you are over the age of 23 i want to let you know that you are invited to the party you are allowed in and how do i want to do that well i want to use this library right over here to print something to the console saying that you are allowed in so what is the expression that we need to create here well the expression is something that looks like this is age greater than or equal to 23 now in this case well again we want to log something so how are we going to do that well we're going to use an if so what's going to happen here is we're saying if the age is greater than 23 then we're going to have curly braces and we're going to run everything inside of this block of code now specifically what i want to do is i want to say uh print line i'm going to say you are allowed in or in like so so if i went ahead and let's just go ahead and run this file so i'm going to say go run 03 and i'm going to say if else else dot go and you can see here we have you are allowed in now if i went ahead and uh change this to 14 this is gonna be false and thus it's not going to hit this block so let's go here you can see here we get nothing now what is the else statement so right after the if we can actually have an else and as you can imagine this is going to be the case where well we don't catch anything inside of the if this is kind of the uh the everything else everything else that happens we'll just catch it over here now the else block essentially means at this point is that hey you are well younger than 23 so we're gonna say friggoff ricky if you guys get that reference let me know it's probably one of the best shows of all time so let's just go ahead here and now you can see we get friggoff okay cool now let's say we have multiple different conditions maybe we just don't have one condition we want to check for multiple conditions well we can use the else if uh statement so let's go ahead and use that i'm just going to go ahead and copy this and let's say for this condition we also want to check if you are older than 20. so if you're older than 20 we want to give you just some some encouragement telling you hey you're almost allowed to come to the party just wait a few years be patient so to do that right after the if statement and before the else i'm going to say else if and then over here we're going to have another condition so we're going to say age is greater than 20 and then let's go ahead and do something here now i believe it's yelling at me because we got to do it like so and over here i'm going to say almost there so what's going to happen here with the else if is is going to go ahead and check the age if the age meets this condition then anything below it is not going to be called so anything below it is not going to be called just going to go ahead and call this however if it doesn't meet this condition then it's going to check this condition and if it meets this condition anything below it is not going to be called and below it doesn't just have to be an else statement we can have multiple else if statements to check multiple conditions and then if well if it doesn't meet this condition and then over here it reaches the else the else is going to be called so let's go ahead and give this a quick go uh so let's go here let's change the age to 25 so 25 it should just reach this block and not call any of the other blocks so let's go here as you can see we got you are allowed in uh we get it twice because we call it here and here so let me just go ahead and comment this out you can see we are allowed in now if i went ahead and changed this to like 22 we get almost there if i change it to 19 you get frig off and that is the basics of if else statements you're going to be using them all over while you're programming life every single programming language utilizes if else statements are very very important now that we talked about if else statements i'm going to talk about switch statements that are very similar so let's say that we have a lot of different conditions that we want to check now in this case we're going to have to create a lot of if else if else statements and you can see that it can get pretty long well in cases like these we can actually use switch statements so switch statements are just very simply a different way where we can perform the exact same thing but syntactically it might be nicer if we have a lot of conditions that we want to check so for example let's go over here and let's create a brand new variable and by the way i created a new file and i just have this boilerplate and let's go ahead and this variable is called animal and i'm going to say animal is equal to cat so depending on what the animal is what i want to do is console.log what that animal would typically say to the terminal now we can have multiple animals we can have cats dogs horses frogs etc and having an if else statement can get really really long this is again where we use a switch statement so we can say switch and then we want to switch on well whatever it is we want to switch on so this is right here the condition then we have curly braces and then over here we say case and then we say cat and then we do colon let me go ahead and tabulate this better and then right below we can go ahead and print out whatever it is that we want so i can say here uh print out meow so what we're doing here is we're checking if the animal if the if the animal is equal to cat then we want to print this out so this is extremely similar to an if statement we can just do if animal is equal to cats then we can go ahead and print out print out meow so print out meow but you can see that this is a little bit cleaner now this is more code uh for now but just watch when we start adding more conditions so over here i can say case dog this time and let's just do prince woof and then over here case let's say frog and let's print out ribbit and lastly let's do case horse and let's print out nay now if we try to do that here we're gonna have to do else if we have to repeat animal every single time say animal is equal to uh dog and then over here we do print new line woof like so now as you can imagine we're gonna have to do i'm not gonna go ahead and type this out i'm just gonna add the equivalent else if statements you can see that this is kind of messy it's not really that elegant whereas this one it's it's it's pretty elegant and it's uh it's definitely less code so this is uh what we probably want to do if we have very simple uh statements that we want to check and we have quite a few of them so let's go ahead and get rid of this now now you might be thinking well what about the default case what if the uh the the animal is not either cat dog horse or frog maybe it's a salamander or whatever well in this case we can have something similar to an else block but what we call we call it default so in this case we can just catch all if we don't catch them here and then we can just say oh i'm not even using uh let's just go ahead and import ftm there we go prince new line prince new line meow let's go ahead and fix that apologies for that and over here in the defaults we can just say don't know the animal that's it and that right there is a switch statement let's go ahead go run and i'm going to say oh for dash dot go you can see we get meow in this case if i were to change this to a horse we should get horse nay that's what we get if i change it to something that we don't know like let's say a bear uh we get don't know that well that animal there we go that's pretty much it in this video what we're gonna do is talk about loops and how we can perform loops inside of golang now what are loops and why do we want to use them well loops are used when we want to run a snippet of code multiple times so let me go ahead and show you an example let's go over here let's create a new file i'm going to call this o5 loops so all file loops dot go and over here let's just copy what we need let's copy all that let's go over here let's close that off okay so let's say what i want to do is i want to print every number from 1 to 10. of course i can do this manually i can just go print a new line 1 and you get the points i can just keep going like so then over here i can just change this to 2 3 4 5 6 7 8 and then 9 10. now you can see here that i am running the same code every single time multiple times now again this is only one line of code but there could be cases where we want to run blocks of code multiple times this right here is where we would ideally use a loop so a loop what it does is it's going to loop through an iteration and it is going to call a block of code and run a block of code based on a condition so what's the condition here well i want to loop from number 1 to 10 and then i want to console this output right over here so let's just go ahead and do this inside of a loop so in a loop what we're going to do is we're going to use the 4 keyword so four now this four keyword is going to take in uh multiple different things so bear with me if this doesn't make much sense the first thing that it's going to take in is a new uh variable that we are going to assign initially i'm going to assign i to be equal to 1 and then we're going to do colon and then the second part is going to be the condition so so long as i is less than or equal to 10 then we want to run this loop and then after we run each loop what i want to do is i want to increase i by one and then over here this is the block of code that we are going to run so i'm going to run fm dot print new line i so let's go ahead and run this i'm going to say go run 05 loops dot go and as you can see here we get every single number from one to ten now let's just go ahead and go through this one more time just in case that this doesn't make sense so over here we have this for loop and this is the only way that we can loop through things in go is by specifying four now the first thing the first part that we're doing is we are creating this variable and we're assigning it to the number one and then over here this is the condition of when to run the loop so as long as i is less than or equal to 10 go ahead and run this loop and by the end of each loop what i want to do is i want to increment i so as soon as this loop ends we're going to increment i from 1 to 2. and eventually i is going to reach 11 it's not going to pass this condition and thus the loop is going to terminate so that's pretty much what we can do with loops very very simple now there are multiple ways that we can actually use for to define our loops so for example what we can do here is instead of having this part right over here we can just go ahead and declare the variable above so we can say j is equal to one and then over here we can just very simply say four and then we can say 4 j j is less than or equal to 10 and then we can run the exact same thing so we can run the exact same thing this time with j of course and then over here we can say j plus plus so this in essence is doing the exact same thing so this and this are pretty much the exact same thing except here we are declaring and assigning inside of the loop whereas this one we're declaring and assigning outside our condition is right over here condition right here and then our increment is right over here so at the end of the logic we are incrementing over here at the end of the loop we are incrementing so again in effect if i comment this out we should still get one two three four five six seven eight nine ten as you can see everything is a-okay starting at one going to ten so that right there is actually known as a while loop because this right here actually could be a little bit dangerous because what could happen here is this condition can never get met so let's say by accident instead of saying j plus plus i say j minus minus and so we are actually going to be decrementing a j and thus is always going to be less than 10 and thus this loop will always and always run and we're going to get some weird funky behavior inside of our machine because our machine is just going to continuously process this loop and it is known as an infinite loop because it's going to continuously run and run obviously this is something that we don't want i could demo it but i don't want my machine to go bad so i'm not going to do that um another way that we can actually define a a for loop it's actually not having the condition here at all so let me go ahead and comment this out let's go ahead and define another variable i'm going to say let's say k and i'm going to say that this is going to be equal to one and we can just say four and then we can just go like this now as you can imagine this is going to cause an infinite loop uh there is no condition so it's going to continuously go and go and go so what i can do here is i can just say prince k so prince k and of course i can do k plus plus but well there is no condition well what i can do if i wanted to is just add the condition right inside of the loop so i can say if let's say k is well let's just continue what we had before so less than less than or equal to and we can probably put this should probably put this above so if k is less than or equal to um uh or actually no let's say if k is greater than 10 in this case then what i want to do is i want to break and this is going to break out of the loop so again in essence exact same thing that we are doing here just syntactically is a little bit different this right here is our condition so let's just go here and as you can see one to ten all right so that is pretty much it uh there is one last thing that i do want to show you i'm gonna go ahead and copy let's go ahead and copy uh you know let's just go ahead and create another loop and this one's going to be just a 4 and let's just say r is less than 100 and for this loop all i want to do is i want to print out um all of the odd numbers i don't want to print out any of the even numbers so there's multiple different ways of course that i can do this i can have an if else statement which i actually will uh so let's go ahead and do that so i'm going to say here i want to print so let's say i want to print new line and i want to print r and over here of course we're going to increment r because then in this case it's kind of it's going to be an infinite loop however if so if r modulo or percent that's what it's called modulo 2 remember we talked about this let me go ahead and get rid of these brackets is equal to zero then what i want to do is i don't even want to hit this block of code right over here i just want to go ahead and move on to the next loop so what i can do here is i can just say r plus plus and then i can say continue continue so don't even hit this block of code just go ahead and continue so let's go ahead and run this and in this case we should get all of the odd numbers and as you can see that is exactly what we get in this video what we're going to do is learn about functions functions are things that allow us to run block of code repeatedly throughout our code so let me go ahead and show you an example i created a new file and let's say in here we're writing some complex algorithm so let me go ahead here i'm coding let's say these comments are code and then right over here in line nine i have to create a loop that counts down from a certain number and then it just prints each number to the console so we've done that before let's go ahead and quickly create that i'm going to say i is equal to let's say 10 and then i'm going to say i is going to be less than 0 that's the condition and then i minus minus and then over here i'm going to do prints line and i'm going to print i like so so over here we have this nice block of code and then i move on i do more coding more coding and then i have to do that exact same uh that exact same feature that exact same piece of logic so what i do is well i just go ahead and implement it let's say this time we have to count down from five and then i go ahead i do more coding you know more and let's say i have to do that feature again logic again and let's say this time it's from 20 so i have to count down from 20. so do you guys see a problem with this because i do we are repeating the code multiple times even though we're counting down by uh by a different number each time the logic is exactly the same so what we can actually do is encapsulate this logic inside of a function just like this one over here and very simply call that function so let's go ahead and define a function so a function is defined by the func keyword and then we can call our function whatever it is that we want i'm going to call it countdown just something nice and descriptive so let's go here let's call it countdown like so and let's actually just make it all lowercase or you know what countdown let's make a camel case because there's two separate words technically okay so over here we have our wonderful function and now what i want to do is i want inside of this function this is the block of code that i want to run however i have no idea what this number could be so when i call this function what i want to do is i want to pass into it a parameter and that is going to be this number because it could be variable so over here let's just say num and i'm going to define this as an integer so i'm going to say this parameter is always going to be an integer then over here instead of hard coding 10 i'm just going to say num so that is pretty much the premise of a function so over here instead of instead of having this for loop what i can do now is i can just call this function like so this is how we call it and then in it we're going to pass in 10 and then over here we can pass in five and then lastly over here we can pass in 20 and that is it so if we go ahead and we run this so let's go here let's do a clear and then i'm going to do go run and then 0 6 and then functions dot go you can see that it is running so you can see it's counting down from 10 to 1 and then here from five to one then here from 20 to one so that right there is the premise of functions so let's go ahead and explore a little bit more about functions so let's say i want a function that is going to sum up two numbers and then return the value well what we can do now is well let's add a add numbers function and this is going to take in two parameters num1 which is going to be an integer and then comma num 2 which is also going to be integer and it's also going to return an integer over here this didn't return anything it just consoled something to the terminal but this is going to return a value so i'm going to specify that it's going to return an integer and very simply what we can do now is just say sum is going to be equal to num1 plus num2 and then i can return using the return keyword the sum so over here let's go ahead and call at number with five and five and because we're returning it we can actually assign this to a variable so let me go ahead and say um the sum is equal to whatever is returned here and then i can say uh f t m dot print new line and i can say the sum like so so let's go ahead and open that up let's run this again and as you can see we get 10 and if i were to remove this return variable then eventu this this this thing isn't going to return anything so let's just see what happens here uh let's just now this is unused uh so yeah so let's just go ahead and just print both of these out so i'm going to say sum like so and it's giving us an error because we're not returning anything so let's just get rid of this you can see here right away we're getting hey there's no value being returned so you can't really assign it to a variable so that's the the premise of the return keyword in this section we are going to talk all about structs so i went ahead and i created a new file and i added the boilerplate now sometimes what we want to do is we want to group data together so so far we have created variables and for example we may have created a variable that is an animal and then we just assign it to a particular value but what if we want to have more information about that animal so for example maybe we want to have the animals the animals class so what class does this animal fall into the animals age the animal's gender things of that nature so what can we do here so let's say we have an animal their name is ted and i can say ted class that is something that i could definitely do just have a new variable for every single one and then over here i can say ted age and then over here i can have 24 and then i can have another variable ted um gender so gender and then over here i'm going to say true for female and then false for male this is a boolean value so you can see this is one way that i can do that and if i want to have another animal we can have another set of variables but you can see that this is kind of messy and right here this block right over here is really just for one particular animal the animal ted and this block over here is for another animal let's say the animal i don't know tuco so what we can do is we can actually group this data together with structs so let's go ahead and work with that so the first thing that we want to do is we want to actually define our struct now to do that we're going to say type and then we're going to say animal so this is a type and we're saying animal with with a capital case because it's a type and then we're going to say struck and then over here we're just going to define the different fields as well as those associated types that we can have for any particular animal so class string age this is going to be an integer and then we're going to have the gender which is going to be a boolean value so that right there is just the type of the struck and now what we can do is we can say var and let's say teddy and we can say that this is going to be equal to animal and then we do curly braces and we define all of the different fields so we can say here class and i can say bear and age and then we can say 24 and then gender and i can say true for female so that is pretty much it very very simple so let's go ahead and just go try to log this to the console so let's do import uh f uh mt let's go fmt dot print new line unless print teddy so let's go here and now what we can do is we can just say go run and we can say 07 structs dot go and you can see we get this structure right over here so we get bare 24 and true so rather than having you know three separate variables we can just group them together into one variable with this structure and what we can actually do that's pretty cool is we don't have to get the whole thing we can actually get a particular field if we want to so let's say i want you know the age field i can go ahead and get that by saying dot h if i want the the class i can say dot class so again if i run that you can see now i just get 24 and then bear as well another thing that's really cool is that we can read and write these so we have read them but we can also change the value of these so i can go here and i can say teddy.age is equal to teddy.age plus1 so he increased in age i can change the class maybe we found out that he wasn't a bear maybe it was something else so we can of course do all these things and if i go ahead and just print this out it should go from 24 to 25 as it does like so okay so that is pretty much the premise uh for the rest of this lecture i'm just going to go ahead and show you different ways that we can define uh structs so let's just go ahead and do that so another way that we can define a struck is let's say we have another animal called leo we can say animal and instead of defining the field we can just straight up define the the values of these fields but they have to be in order so they have to be class h and then gender so over here i can say lion so lion i can say two and i can say false for mail i'm going to go ahead and just grab this log here and just so we don't have an unused variable let's just go log leo so this is the exact same way it's going to result in the exact same thing now if you want to for some reason then there's probably no reason you want to do this but you want to switch up the order well in that case this is where you would want to use the field names over here you can see the order doesn't matter because we're specifying the field name whereas here the order does matter because we are not specifying the field name you can see it's yelling about the types because it's expecting that the second thing is going to be an integer but we're providing it with a boolean okay so that is just another way we can do things so that is looking a okay another thing that we can actually do is we can just say let's say another animal let's say lalo and we can just say animal like so and then over here we can just provide absolutely nothing so let's just say lalo and what ends up happening is like a string it's just going to provide the default values so the f the field class is just going to be an empty string the gender is going to be false and then the age is going to be 0. later on what we can do is very simply just say lalo dot class is equal to i don't know let's just say lion again so just to prove to you that uh this is what we get you can see here we got lion zero false now if i don't assign this we should just get an empty string which we don't really see here okay uh so that that's uh pretty much it uh another way that we can define it is by not actually using the type we can just use an anonymous structure and we can define it by doing something like this so we can say var let's say tuko i'm using better call saul characters now if you ever watch that show you can just say struck and then we can say class string and then age and then integer and then gender uh boolean so now we're not using any animal we're just defining it as is and as you can imagine this is completely fine uh what's going to end up happening is it's just going to use the defaults now if you want to create this anonymous structure and assign the fields values you can just do do it like this so you can say class whatever and then uh we can say we can say uh age is whatever and then we can say the gender we have a syntax error here actually no i don't think it works that way with var but what we can do is if we actually change this to um [Music] if we change this to the create and assign like so that's when we can do that so over here now we can define it so we can just say penguin let's say and then age is one and then gender is false all right and that is it so we can end that off with a comma and i'm not going to go ahead and bother console.logging it i hope you guys get the point okay so those are structs in this section we are gonna talk about arrays arrays are ways where we can store multiple units of the same data so for example let's say i want to track all my purchases and i want to store them somewhere well i would do that inside of an array so let me go ahead and demo exactly how over here inside of my main function let's say again i want to store all of my purchases just so i can be you know more cognizant of what i am spending now in order to do that what i would do is i would say purchases and then colon equals to assign and to declare and then i would say well these brackets right over here so this is going to signify an array and then inside of the bracket i'm going to specify how many items i want to store in here so essentially how many purchases i have made so i'm gonna say five purchases and then right after that i'm going to specify the type because you can only store one specific type inside of an array you can't have an array that has boolean values and then another value is an integer value etc now for this because they're purchases and we're dealing with money i'm going to say float so i can have decimal points and then well that is just defining how many floats we have in our array but now we actually need to assign our values to do that we do curly braces and then we just start assigning our values so let's say i spent 1999 and then i do comma uh 2099 and then i do 599 and i do 199 and then i do 14.99 so that is how we do that that's how we store our array now if i go ahead and i do an a print let's go ahead and print that out just see how that looks like let's go here let's close these two off let's just clear this and let's go o8 arrays and now when i run that you can see we get a list an array of purchases that i have made each particular purchase each particular item is a float value now what's nice about arrays is i can access specific uh points so over here what i can say is maybe i want to get item three or let's just say purchase three so let's say i want to get this third one what i can do is i can say that is going to be equal to and i can create and assign this that is going to be equal to purchases and then i specify the index of that particular element now the index is the location where that element lives in the array so in go we always start with a zero index so this very first item right over here has an index of zero and then this one has an index of one this one has an index of two three four so over here if i want to get this third element then i'm going to get it at the index of two because remember zero one two so let's go here and let's just say purchase three and now i can access that element as you can see right there now let's say i made a mistake i put 599 when it really should have been 15.99 so i can actually reassign this particular value in the array at this index so to do that i can just say purchases at index of 2 i want to reassign it to 1599 and now if i were to run that you can see it is 1599. okay now another way that we can create an array is by using the var keyword and so this will allow us to create an array without assigning it to anything so i can say var and let's just say sales and i can say income or sorry var inc sorry var sales is going to be equal to and then over here i can specify let's say 4 and then float just like so so that is relatively simple something that i can do as you can see my cat being a little crazy so let's go here and wow he's insane he's just running around now like a mad madman all right so let's see what's going on here so expected expression var sales or rather sorry this is how you do it like this so like that and then what we can do here is we can just say sales uh of let's say one or let's say starting starting with zero is equal to whatever this number is now we have specified the length of the array is going to be 4 and but we only assigned one value the first one so what do you think is going to happen here well as you might imagine and this is what go has been doing is it's just going to give the other three default values so soul is going to assign them to something they're just going to be default values now if this bothers you here and you don't know how many elements that you want to have in your array what you can very simply do is just say dot dot dot and right there what that does and i'm just going to go ahead and this only works when we don't use var we use to create and assign so let's just say sales and then over here i'm going to say create and assign and i'm going to say over here dot dot dot and then over here we can specify all of the different um array elements just of course still specify the type and then over here we can just put whatever it is that we want so what this does is it just calculates exactly how much we have here and it just puts it in that value okay now the last thing that i want to talk about is iterating through an array so we've used loops to iterate over you know numbers and then execute the same code more often than not when you use loops you are iterating over array and you're doing a particular action on each particular element so let's say over here i want to iterate over my purchases and i want to log them to the console so let's go here i'm going to say 4 i'm going to say let i is equal to 0 starting at the index of 0 and then so long as i is less than so we want i to be less than the overall length of this array because if it's less than the overall length of the the array which is one two three four five that means at the point four it could iterate through it'll continue the uh loop but at five which doesn't have an index that means it is going to stop so as long as it is less than the length of the array and this method right over here is going to allow us to calculate the length of the array so this right here is going to resolve to five and then over here we can just say i plus plus we can execute some logic so let's just go here let's just say print new line and i can say i so let's go ahead and run this you can see we get 0 1 2 3 4. uh sorry that's because we do not want to print i we want to actually print the purchases of i so let's go here because this right here is simply the index we just want to get the purchases of that index and there we go that is exactly what we get in this section of the video we are going to learn about slices slices are extremely similar to an array except they have a variable length let me show you exactly what i mean so let's go over here to the arrays now as you can see here we have created this array and we have specified that it has a maximum length of five now this could be potentially problematic let's say i want to add another purchase i could make multiple purchases in the future so say i made another purchase and i want to append it to this array well how do i do that well over here the last element has an index of 4 so you might be thinking i could do something like purchases of index of 5 is equal to 14.933 let's say well this is not going to work as you can see we have this little error line if i hover over it you can see that the error that we're getting is that it is out of bound and that's because well we can only have five elements by doing this we are adding another element increasing the length to six now of course i can change this to something like three where we reassign the values but again we cannot do that well again this is very problematic so what can we do to fix this well instead what we can do is we can just grab a slice of that array so i'm going to go here and i am going to just copy this array and let's just go here and say my slice and i'm going to assign it to the value of purchases and then what i am going to do is just have the brackets and then colon so colon like so so what this is going to do is going to get a slice of this array and over here over here in this colon we're basically saying that we want every single element inside of this array to be put into this slice so if i went ahead and did an fm print line and then i can say my slice let's just go ahead and run 09 slices you can see we have a slice and it looks really really similar however now we can do some really cool things with this so over here let's say i want to add another value what i can do is i can reassign this to my slice is equal to a function call that is going to be called append and then i'm going to have my slice as the first variable and then the second second sorry the first parameter and then the second parameters are going to be just the uh float values that we want to append to my slices so over here i can say let's say 14.99 i made another purchase 15.99 and i can say 19.99 like so and now let's open up our terminal again you can see that if i were to run this you can see now we have those values in there this is something that we cannot do with an array now let's say we do not we just really just want to define a slice right away and we do not want to create this array and then create the slice what we can do is we can actually do the both of them at the same time so over here let me just comment this out over here and let me just put purchases here what we can do is we can say instead of specifying any number we can just say an empty array what this is going to do is create an array and then it's going to take a slice of that array and then assign it to this variable so that is just one way we can do it just to avoid this additional um creation of the ray and then creation of the slice okay now let's talk about this syntax a little bit more this right here just to really understand it so let's say i want to have another slice so my other slice and let's say i want to have the slice from uh from here so this number this number and then this number so these three numbers so those are the slices that i want well what i can do here is i can say purchases and then i can say i want it from starting from zero so starting from zero the index of zero and then i'm going to say index of well we want index of 2 so we're going to say index of 3. now this might not make much sense but the first number is going to actually include this particular element the first element that we want to slice however the last element the last index is not going to include this one so it's exclusive so it's going to be 2. so if i want from index 0 to 2 i'm going to say 0 to 3 because 0 is going to be inclusive 3 is going to be exclusive so that way we get these three elements now of course if i want it from one to two i'm gonna say one to three because that is one again is going to be inclusive three is gonna be exclusive we're gonna get this so i'm going to go back to zero let's go ahead and print this now my other slice let's open up the terminal now you can see here we have these three values looking good looking good now because we are starting from the very beginning of the array we can actually um we can actually uh simplify this by simply removing the zero so this is saying that well start at two i just want everything from two and then to the front so starting from the very beginning of the array go from the very beginning with the array to the index of two that is what it is saying so let's go ahead and define another slice and let's say for this slice i want to get um let's say this element right over here and then every other element after it so over here i'm going to say my third slice i'm going to say that that is going to be equal to i'm going to say purchase and then i can say well 2 2 and then that is basically what i can say here so i can say from 2 so index of 2 because it's inclusive and i want everything afterwards so that is why i'm not specifying anything after that so i can go here my third slice my and i didn't define it i forgot to do that my third slice if i were to run this you can see we start out here and we get everything after words looking good looking good and i hope that makes sense why we only have a colon here because colon here is saying okay we just want everything from the start of the array to the end of the array now the last thing i want to show you is we have used this append to start adding things together we can also use a pen to combine two slices together so let's say i want to combine two slices together so i'm going to say append and then i'm going to say my other slice and then i'm going to say dot dot dot or sorry my third slice dot dot dot so what that is going to do is we're going to have my other slice and then my third slice dot dot dot whatever values are in my third slice which are these values right over here it's just going to spread them in there so this is equivalent to this right here so this is equivalent to this all right i hope that makes sense and just for proof let's just go ahead and print this out i'm going gonna go ahead and try to just print out one so just combine let's see if we can remove my slice yes okay looking good so let's just go ahead and print out combine and that's just gonna combine those two together in this section of the course we're going to talk about another data structure called maps maps are a way where we can store data in a key value pair fashion so let's go ahead and take a look at an example so let's say in my program i have some sort of shopping cart and what i want to do is i want to store the item that is in our shopping cart as well as the quantity so it could look a little something like this so let's say i want to buy a lamp and then the quantity could be let's say three and also i have a bowl and the quantity in this case is one and then over here i also have a laptop the quantity is always one in this case you can take kind of take a look at this data structure and can see that this value is assigned to this value over here they're both related so this right here is a key value pair this is the key and this is the value of the pair so we have key value key value now in case of maps and this is what we are going to create with the map the key always has to be the exact same type in this case it's a string and then the value also has to be the exact same type in this case it is an integer now the key doesn't always have to be a string it could be an integer could be even a boolean value for some reason and then the value over here could be anything as well so long as they are all the same so let's go ahead and define the shopping cart so what i'm going to do is i'm going to say cart i'm going to go ahead declare an assign i'm going to call the make function to make my map now in here i'm going to specify that i want to create a map and then i'm going to specify the types of the key value pairs so i'm going to say string so inside of these square brackets that's going to be string and that's going to be for the key and then it's going to be int for the value so that's outside of the square brackets so we have just created our ma our map our cart map but now what we want to do is we want to start adding values to it so the first thing that we can do with a map is write things to it so i can say here cart of lamp so over here i'm assigning the key with these square brackets is going to have the value of 3. i can go over here and say of bowl is going to have the value of one and let me go ahead and just print this out so let's print this out sprint out this cart just to see how it looks like and let's just go go actually i think i have that here go run maps you can see we get this nice little data structure where we have the key the value the key the value looking good looking good another thing that we can actually do is something like this let's say cart laptop and then i can say plus equals one so whatever this is originally equal to add one to it now you might be thinking well we don't have anything that is equal to laptop so what it's going to do is it's going to assign this value to 0 the default integer value and then add one to it so if i go here you can see we have if i were to save it you can see we should have laptop now okay so that is writing to it now let's talk about reading to it so over here let's say i want to access the lamp so they want to ask us how much we have in here so i can say here lap lamp quantity and i'm going to say that that is going to be equal to cart of lamp very simple over here if i were to do that just say lamp quantity of course i have to create and assign here i should get three as i do now another cool thing about this is it's actually going to give us two different variables so it's going to give us this over here which is the value but i can also access another variable we can call it whatever it is that we want but i'm going to call this found and what this does is it gives us the boolean value as to whether this key value pair actually exists in the map so over here in this case it does exist so let's go here and let's go ahead and say you can see here that it says true however if it doesn't exist so let's just say i say book for some reason it's going to give me it's going to give me the default value which is 0 and then it's going to give me false it doesn't exist now what do you think is going to happen if i actually go ahead and create a book with a value of zero so i'm going to say book i'm going to say equal to zero if i were to do it now you can see it gives me the value of zero but this time it gives me true because it actually does exist inside of my cart i just assigned it to zero all right so that is reading so reading the last thing that we got to talk about is how we can delete key value pairs and this one is pretty easy as well so to delete key value pairs we're just going to call the delete keyword pass in the map where we want to delete something and then we pass in the key so let's say i want to delete laptop that's all we have to do very simple so let's go ahead and run this again now you can see we do not have a laptop anymore now one thing that is very important to understand are these things aren't ordered so if you decide to iterate over them you might get a different order for each time so let's say you iterate over them the first thing that you get is lamp then you get laptop then you get book then you get bowl and then you iterate over them again maybe next time we're gonna get book bowl laptop lamp so it's different from an array or a slice where the elements are ordered so over here they're just key value pairs and they are not ordered the last thing that i want to discuss are pointers so to describe pointers let's go take a look at an example so let's say i declare a variable a and i assign it to the value of one i then declare another variable and then i assign it to the value of a so let's just go ahead and print this out just to see what it looks like originally now as you can probably imagine a is going to be 1 and then b is also going to be one as well so let's just go ahead here let's print that let's do a quick clear print it again and then you can see here we have one one now what is going to happen when i increment b by like one so how what's gonna happen here let's go ahead and print this out and as you might expect b changes to two however a stays at one and that's because when we declare in this fashion what ends up happening is we get a copy of a and we assign it to this value b so this is a whole new piece of data it's not linked to it in any way however sometimes we want to link a particular variable to an existing variable and this is where pointers come into place so let's go over here i'm going to say var c and i'm going to say that this is equal to 1. i am then going to assign another variable var d however this time i'm not going to straight up assign it to c what i'm going to do is i'm going to specify that it is going to be an integer and then i'm going to say star and what this means is that this is going to be a pointer it's going to point to a particular variable and this variable is going to be an integer i can then if i want to say d is equal to and then if i want to specify the pointer i say the and sign and then c like so so we have var d and then over here we have specified that it is going to be a pointer and then we say d is equal to the and sine uh c now this is giving us an issue so over here we're saying d is declared but not used that's okay we can just use it over here so now what do you think is going to happen when i decide to increment d so let's go over here and i'm going to say actually you know let's get rid of it from the console i am going to go ahead and increment d now if you want to increment a pointer you're going to say star d the pointer d plus equals and then 1. so let's just go ahead and log what actually c is in this case so in this case c is actually going to be 2. even though we have not worked directly with it because we are pointing to it and then we are mutating it you can see that c is now uh c is now two now why would we ever want to do this well we want to do this if we do not want to store this other piece of data again in memory because remember when we do this over here what we are doing is we are creating a copy now if we do not want to create a copy we can just very simply point to it and it's just going to point to the original variable that is already stored in memory so we don't have to store any more data in memory and this is ideal when we're passing things to a function instead of passing the parameter as is which would create a copy we can just say point to it and if the parameter is some large array we're saving a lot of space in memory
Info
Channel: Laith Academy
Views: 13,861
Rating: undefined out of 5
Keywords:
Id: 50ewcV8PsI4
Channel Id: undefined
Length: 103min 20sec (6200 seconds)
Published: Mon Jun 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.