Go / Golang Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] this video is sponsored by dev Mountain if you're interested in learning web development iOS or UX design dev Mountain is a 12-week design and development boot camp intended to get you a full-time position in the industry to learn more visit dev mountain comm or click the link in the description below hey what's going on guys welcome to my go programming language crash course and in this course we're gonna look at all the fundamentals of go we're gonna set up a workspace properly and look at things like variables arrays and slices pointers conditionals just all the core things that you need to know to begin to write applications with this language and go is extremely efficient extremely fast it was it was created by Google it's used for a whole bunch of stuff including web development I do plan on doing more videos where we actually build some web apps some back-end web apps using go but I want this to be kind of an intro to the language so this would be a first stop when wanting to learn this language alright so first thing you need to do is install it so if you go to golang.org click on this button right here and just choose you know whatever you use Windows Mac Linux I'm on a Mac so I would choose this but I already have it installed because it's a very self-explanatory process just like installing anything and then once you have it installed just go ahead and open up a terminal or your command prompt in Windows or get bash whatever it is you use and just do a go version and you should see something like this that'll let you know that it's installed correctly alright so first topic I want to cover is the workspace because this is very important it's a very opinionated language when it comes to how to set up your file and folder structure so this page is from the documentation if I remember I'll put it in the description and it kind of shows you how to create a workspace says right here go programmers typically keep all their go code in a single workspace and that includes your project code as well as any third-party packages that you install because with go you have a command called go get in fact they'll go ahead and say and go help this shows us all the different commands you can see there's a get command to download and install packages and dependencies and there's a website called go dot org where you can see all the the packages you can search for them some of them are hosted on github some are on like golang.org and so on alright and I'll show you how to install one of these in a little bit but down here you can see your workspace should have a source folder and a bin folder and it kind of gives you an idea of what it should look like down here but I created a text file you just open that real quick that kind of really shows you how your workspace should be set up so you have your main folder okay so this is in this case it's called go you have your bin and your source so binaries in your source code package this is for if you install third-party packages it'll create this folder but your source folders your your main folder and in here you want to put a folder like github com if you're going to use github of course you could use like get lab or something like that but you want to use version control excuse me version control with your go projects okay so github calm and then a folder with whatever your github username is and then in there as well you put your projects okay so you see go project one go project two and so on and then when you install a package if it's hosted at github for example it'll actually get put in this github calm folder ok so that's the basic idea of how how your your go projects should be structured I apologize if you can hear the rain it's it's pouring out there I actually just got a flash flood warning on my phone but anyway so let's create a workspace okay now if I clear this up and I do a go env this shows me my go environment ok and what I want you to look at is the go path and by default that's going to be in your home directory so users whatever slash go and you can change this if you want you can make your workspace anywhere but you need to make sure that you set the go path environment variable ok and it's different if you're on Windows or Mac or Linux I'm not going to go through that I'm just going to use the default workspace okay so in my home directory I'm going to create a directory called go and you don't have to use your terminal for this you can use your finder or Windows Explorer or whatever you want but let's make a directory called go let's CD into go let's make two directories so one has been and one is source and let's CD into source and then let's create a directory called github comm CD into github calm and then make a directory with your github username in my case is Brad Travis e and then I'm gonna CD into that folder and then this is where we create our project so I'm gonna make a directory called go underscore crash underscore course okay and the idea of this project is just to have a bunch of folders with a go file in it and for instance we'll have like 0 1 hello for the hello world project 0 2 variables for variables you know 0 3 arrays or whatever and then you guys will have that as a reference I'm gonna put this into a git repository and then I'll put the link in the description for you guys if you're not following along so let's CD into go crash course alright and in here I'm just going to open up my text editor in this folder I'm using Visual Studio code so I can say code dot which will open the current directory that I'm in ok so you can see over here I've go crash course open now if you're using vs code I would suggest hitting the extension icon here and searching for the go extension you can see I have it installed right here this will help with like intellisense and you know give you dropdowns for methods and stuff like that you can also do testing and diagnostics I would definitely recommend it and when you create a go file you might see some pop-ups to install certain packages that can help you out and I would suggest installing those as well alright now before I create anything over here I just want to show you how to install something using GoGet so from go org I'm just going to grab this right here so that get blink and then we'll go to the terminal okay it doesn't matter where you are on the terminal you can run this from anywhere and go get and then let's go ahead and paste that link in and run it okay so this is kind of like doing an NPM install or a pip install or something like that all right now I'm gonna go to my finder and go to my go workspace that we created and notice it created a package folder but also if we look in source and github.com there's the AWS package that we just installed because it's coming from github it's gonna get put in the github folder so you can see that the structure is very very opinionated if we installed something from golang it would install who are I'm sorry it would create a golang.org folder right here and the source automatically alright so hopefully that gives you an idea of how the workspaces work all right so now let's jump into vs code and I'm gonna create a folder here I'm going to call it zero one underscore hello because I just want to do a simple hello world and I'm gonna create a file called main go okay main is the common convention for the the entry point the main file it's like an index.php or you know an index.html whatever so let's see down here you may see some pop-ups once you create the file if not click on this analysis tools missing and it's going to ask you to install some packages I'm gonna click install and if we look at our workspace now inside github.com you'll see that it's installing some stuff here that's gonna help that'll help us out in the editor also some stuff is from golang.org like I said so it'll create a folder itself but yeah so those will get installed just we'll just let that run so let's create our first go file alright now all I want this to do is just I just want it to be a simple hello world so in go you have a main function okay it's a function called main and it runs automatically it's kind of like in Java c-sharp and a lot of those other languages but to use the to create the the main function we need to do package mein at the top okay then we need to declare the function with funk mein I'm gonna do a section on functions so I'll explain that a little later on but we're just creating a function called main and this will run automatically we don't have to come down here and say like main or anything like that now I just wanted to output hello world in the console to do that we actually need to import a package called fmt okay I think it stands for formatter or format but this has a couple methods to be able to print stuff on the screen now when you import stuff when you do anything with with quotes you want to use double quotes okay you'll get an error for use single quotes and then down here let's take that formatter I call it the format or I don't know if that's correct or not but let's say print line okay so we have print print format and print line print we'll just do an inline it won't put push the next one you know the next thing on to the next line so we're going to use print line and then in here let's say hello world okay we'll save that and that's our hello world okay now to run this I'm gonna go to my integrated terminal but of course you can use any terminal or command line and then you want to let's see we're gonna go into 0 1 hello and to run this we use the go run command so go run and then the name of the file which is main dot go and there we go we get hello world ok so before we move on here I just want to quickly show you how to compile and create a binary ok create an executable file so for that we can simply say go install ok we don't get any output here but I want to show you that if we go to our workspace just go back home we'll go into the go folder and now go into the bin folder and you'll see 0 1 hello it's an actual executable that you can run from the terminal so if I were to open a terminal in my bin folder here and let's do an LS and you can see the 0 1 hello I can do dot slash meaning the current directory 0 1 under school hello and run it and we get hello world so it actually runs our program so it's that easy to compile and create an executable with go alright so let's move on here we did our hello world so now I'm going to create a new folder I'm going to call it zero to underscore VARs because I want to talk about variables and data types we're gonna create a file called main go here okay and I just want to my probably should have mentioned this earlier but if you don't want to follow along and like create all these files and stuff or even set up the environment there's something called the go playground I spelt that wrong but it's fine so it's play golang.org and you can just run go code in this this GUI right here if you want if you don't want to set everything up I probably should have said that earlier but whatever so let's see we're gonna grab this stuff from the hello project and put it in here and let's talk about datatypes before we create variables so I'm gonna actually pay something in here okay so these are some of them I think I don't know if these are all the types but these are most of them and a lot of these we won't even use but the most important is string bool for boolean int integer for floats or decimals we have float 32 and float64 float64 is is the one we usually use and we have different iterations of int so in 8 through 64 which just it corresponds to the length of the integer ok and then an unsigned int is going to be 0 plus so no negative numbers and that also goes 8 through 64 and a byte is just an alias for an unsigned int 8 ok and arune is an alias for an int 32 and then we have float 32 float64 and then complexes for like really large numbers okay so let's go ahead and create a variable so there's a couple ways to do it first one I'm going to show you is using the VAR keyword okay so let's do var name I'm gonna say this is gonna be a string type and it's gonna equal value okay make sure you use double double quotes here and no semicolons okay and then let's go ahead and take the format and let's do a print line and let's print out name and save this okay now to run this we have to CD into zero to underscore VARs and then just run them the go ranked go run main go and we get Brad all right now you notice in vs code we have this little green line it's not an error it's kind of just like an info information block and it says you should omit the type string from the declaration because it's inferred that it's inferred okay so basically it knows that this is going to be a string even if I don't do this so I don't actually have to put in string in this situation so if I save you'll see if I run it it does the same thing okay so if you're creating a string you don't technically need to add string there okay now if we do let's say our age equals let's actually do var age int equals 37 now I want if I save this we're going to get an error okay so this error is because we're not using the variable and go when you create a variable you actually have to use it so you age declared and not used and if we run this file we get the same error okay so if we go down here to the print and I throw in comma age to print them both out then that error goes away and we can run it okay now we get the same green line we got before because again it's gonna it's going to infer that this is gonna be an int so I don't even need to do that and I'll save and run and there we go now I want to show you how to get the type so to get the type we're going to use for FM t dot print F and print F will take in something called a point a format receiver I believe it's called I'm actually gonna search for go fmt and it's this go fmt and always I said no that's not it this is right here the first one I think this isn't yeah so down here we have different verbs that we can use verbs or format pointers I forget what these are called but they're for different things like if you want to use a boolean you would use percent underscore t for integers you have if you have different ones for different bases like base two as percent B base ten as percent D and so on but what I'm looking for is this one right here percent upper case T and it says a ghost in tax representation of the type of the value okay so that's what we want to use to get the type so in here we'll say percent upper case T and then I'm just going to do newline so so /n and second parameter will be what we want to find the type of so let's do will say name okay what's going on here I'll forgot the T all right so let's let's clear this out and run it and we get our our Brad 37 that's coming from this line and then we get string because it's telling us what type name is we put age in here and we run that we get int okay now if I wanted to specifically cast this age as let's say an int 32 then I would do that here so we can say int 32 and save and now let's take a look at the type of age and it's now in 32 okay so if you want to specifically change it to a different data type you can do that alright so let's see let's let's look at let's look at Const okay so I'm going to take that age right and I'm gonna actually let's do let's do something different will do var will say is cool and set it to true if I save this we'll get an error because we're actually using the variable so I'll just put in here is cool all right and then let's actually look at the type I'm sure you guys can guess what type that's gonna be so if I run this we get wool okay because it's a boolean now I can take this is cool right here and I can then say that I want it to be false okay so if we go ahead and run this you can see that now it's false so I can do that because I use var but if I use Const and this is just like JavaScript es6 if I use Const and you'll see we already have an error here cannot assign to his cool like run it we get the same error because it's a constant it means that you can't redefine it okay so I'll just get rid of that so that's Constanta look at the second way to create a variable and that's the shorthand method now this method you can only use within a function and I'm gonna actually give you an example of that let's take let's take the name here I'm just going to comment it copy it and comment it out and put it outside of the main function like that and then run this file and it still works so it's fine to just clear the name up here and you know a global variable and use it down here now the shorthand method that I'm going to show you is without the var so just name and then colon equals and then whatever you want to assign it so let's say Brad all right now if I save this you'll see we get an error and it says non declaration statement outside function body so this this type of assignment we cannot do outside of a function so I'm just going to copy it or cut it and then put this down here let's put a comment here and I'll just say shorthand and save and now we get no errors okay it runs everything's fine and it's going to assume that it's a string if I if I check the type of it here let me say name it should show us that it's a string okay so you'll see this quite a bit okay unless of course it's outside of a function or needs to be like a different type of different data type or whatever alright I didn't do any floats so let's do like I will just say size and set that to like 1.3 and if we look at the type of size you just show you real quick there is no actual just float it's gonna be a float 64 okay that's gonna be that's what's inferred if you wanted to specifically say a float 32 then you could go up here and say like var size float 32 equals and then whatever a 2.3 then we'll get rid of that okay we'll save that run it and you can see now it's a float 32 all right so I think that's going to be it for variables guys they don't want to waste too much time oh one more thing I want to show you if you have this you can actually do kind of save some time here like if we had email and it'll say Brad Gmail so instead of doing this we can actually shorten this up and we can say name email and then do our assignment to Brad okay so we could do that instead so I'm gonna just comment that out email will throw us an error if we don't use it so let's throw it in here and save and run and it should work okay you can see that that still works so just a shorter way to assign variables alright so I'm going to close that up and let's create a new folder here and I'm going to call this zero three packages okay I want to talk about packages so let's create a file called main dot go let's clear this up and let's copy our initial hello world okay now I've showed you how we can import a core package like FMT if you want to import more than one then you want to wrap this in parentheses okay and then you don't want to put a comma here no comma so we'll just put a space and then let's bring in the math package now notice the different colors here in vs code it's because this isn't formatted correctly what you want to do is put these on separate lines so I could do that but you're more than likely gonna see it like this I have prettier installed the extension prettier 4 vs code so it'll automatically format it like this when I save but yeah this is how you import it now if I go ahead and save this file I don't know if it's a vs code thing or if it does this in every editor but the package will disappear if I'm not using it anywhere okay so I'm gonna undo that but yeah if I save it goes away so let's go ahead and use it so down here let's say print line will take math and let's use floor which will round whatever we pass in down so we'll say 2.7 okay so if we go down here and we run that we need to actually go into that directory CD out of that and then CD in 203 packages and we'll run main dot go and we get to okay we also have the seal method so we can do seal 2.7 and we should get 3 ok so we get 3 you can do like what else let's do let's do square root so capital s Q R T and let's do 16 okay I have to run that we get four all right I don't want to spend too much time on the math package that's not the point of this it's just to show you how to bring in a package and use it now we can create our own packages as well so if I would create a folder here I'm gonna call it STR util for string utilities and then create a file here I'm going to call it reverse dot go and I want this to be a function that returns a string I'm sorry returns a reverse string or reverses a string now we have to claim this as a package and you'll see vs code will actually look at the folder name and give you that as an option and that's what we want okay now for the function I'm just gonna go ahead and search for golang reverse string and this first stackoverflow post has a good answer right here so we'll grab that and just paste that in alright so now we have this reverse dot go ignore this little info thing we'll get into functions later but now we should be able to import this so we're gonna go down here on this next line and let's say github we want to start from the source folder so github.com slash brad travesti or whatever you our user name is and then the go crash course and then what is it zero three packages and then STR util so that should bring it in and then I should be able to use it so I'll do a fmt dot print line let's do a print line and we'll take the STR util and we should be able to say dot reverse and let's pass in a string of hello backwards all right so now when we run this it should give us hello and there we go so we get hello if we were to change this to hello and we run it it should now give us hello backwards because it's going to reverse it so that's how you can create your own package you can also like I showed you install packages with GoGet and then you can simply import them the same exact way so you would do github / AWS and you know the one that I downloaded or whatever and then whenever you want to bring in all right so that's packages like I said I'm gonna be moving kind of fast here because there's a lot to cover so let's create zero four and this is going to be functions okay so it's a new file main go let's copy our initial hello world and I'm gonna create a new function up here alright so this is this is actually pretty simple if you know any other language a lot of this stuff is going to be really easy like the conditionals and functions the loops it's very it's very simple so let's create a function with the func keyword I'll call it greeting and I want this to take in a parameter of a name so we'll pass in name now we want to put the type of this which should be a string and then we also want to put the return type okay so string for the return and if you don't have a return type you can just leave that blank but what I want to do here is return you want to use double quotes and we'll just say hello and then let's concatenate okay it's the same as JavaScript concatenation you just whoops use the what am i doing hold on a second here what did I do I went to the different file just like JavaScript you can use a plus sign and then you know put the variable name so down here let's go ahead and let's run that so actually we want we want to do a format print line because we're not we're not doing any output here we're just returning a value so we have to wrap this in print line and we'll say greeting and let's run this so let's go let's go into the correct folder functions and let's run main go not enough arguments that's because I forgot the name okay run it again and we get hello Brad okay so very simple function let's create one more so we'll call this one get some okay so this is gonna take in two numbers obviously so we'll say num1 which will be an INT and num2 which will be an int and it's going to return an int okay and then we're gonna say return num1 plus num2 and then let's go down here and replace this we have to get some and let's pass in say 3 & 4 save that and let's run it and we get 7 ok so pretty simple now if you want to shorten this up a little bit since these are both intz we don't need the first one we can just do num1 comma num to int and that should still work with no problem all right so I mean there's this there's other advanced stuff you can get into you know advanced functions but I want to keep this light this is just an introductory course so I'm just covering the basics here plus we don't I don't want this to be a three-hour video so let's create on the next folder which is going to be 0 5 arrays underscore slices okay we're going to talk about arrays and slices in this one so main dot go all right and I will be pushing this to get in fact I'm just gonna initialize my repository now I'm not gonna do my commits and stuff till after but just wanted to initialize it so let's see we're going to first look at arrays so let's copy our hello world here and let's see we'll get rid of that will say arrays now with go you're rays have to be a fixed length okay and you have to name the types as well that can be a problem because you don't always know your helmet how many values are gonna be in your array so that's our slices comes in which slice is basically an array that doesn't have a fixed type but let's start with just arrays so it's a fruit array and we're gonna set this to have two values and it'll be string okay actually don't need a space here and then down here let's say assign values so we'll say fruit array let's take the zero index and assign it to a string of Apple and then we'll copy that down this one here we'll say orange say 1 and then let's print it out so format dot print line and say fruit array and save okay I'm just going to navigate to the correct folder and let's run the go file and we get apple and orange okay if we want to specify a certain one of course we could just put the index in zero base just like most other languages so the one spot is gonna be the what the second value so if we run that we get orange okay let's see if I were to actually I want to show you how we can declare the array and assign these at the same time so let's say declare and assign and I'm just gonna comment these out okay so to declare an assign we can say fruits or fruit array no var and then let's do our colon equals and we're gonna say two string and then let's put in some curly braces and then we'll put in our values okay so if we save that and we run this we get the same thing so this is the same as what we did here just a shorter way of doing it so now let's take a look at slices so I'm gonna comment these out and I mean a slices it's pretty much the same thing so we could just kind of copy this and let's call it fruit slice and then just take away the two right here okay so Apple orange let's put another one in like grape to clear yeah so I'm just getting a declared and not used error so I want to print this okay we'll run that and you can see we have all three there and just to show you if it was in an array that was that had two values and we run this we get an error that says index two is out of bounds K so this is index two zero one two and it says it's out of bounds okay so I'll save that let's see I think that's pretty much it I don't want to go over like specific methods and stuff like that if you want to count the number of stuff in the array or in the slice you can use Len so if we wrap this in Len and we run that you'll see we get three okay because there's three values in there if you want to get like a range what we could do is format dot print line and we could say fruit slice and let's say we want one : two and run that and it just gives us orange okay and the reason for that is it starts at one and then it stops at two or before actually before two it does it it's not going to give us the great so if I put another one here like let's say cherry and we do one three we should get orange and grape okay so you can see that right here all right so let's clear this up and let's create a new folder and I'll try to remember to timestamp these as well I'm going to do 0-6 conditionals okay so let's create Maine dot go and we'll grab this okay so conditionals pretty easy if you know like I said if you know any other language this is very similar especially if it's a C syntax language we're going to assign a couple variables here let's say x equals 5 and y equals 10 and then we'll do an if statement now it doesn't common practices to not use parenthesis like if we say X is less than Y like that but if you use parentheses here it doesn't throw an error anything it still works but I think common convention is to not use them so let's say if this is less than Y let's do a format dot print F and in here what I'm gonna do is say % D which is used for base 10 in switch which is what these are and say is less than % D so these are like placeholders and if you've used print at first source print F in other languages you probably know how this works so now we'll put in x and y okay so basically this is a placeholder for the first one here ax this is a placeholder for the next so it should say X is less than Y okay and that would be correct because that's the condition so let's see D into the right place and run main go and we get 5 is less than 10 oh wait a minute Oh see I didn't put a new line here that's the reason for the /n cuz you can see right here it's tit didn't push this on to the next line so we want to make sure we do that and we get 5 is less than 10 which is true so if we change the X to 15 and run it I'm sure you guys know what's going to happen nothing because this is just a simple if now if we put an else here and we can just copy this and let's just simply change the place places of x and y because now y will come first okay we'll go ahead and run it and we get 10 is less than 15 so we can use greater than less than of course we can use less than or equal to greater than or equal to so let's say if X is less than or equal to Y then is less than or equal or equal to and then we'll set these equal and run it and we get 10 is less than or equal to 10 okay so very simple let me just put a comment here we'll say if-else let's get out here and now let's look at an else if so what I'm gonna do is create a variable called color and set it to a string of red all right and then we'll say if color is equal to red then let's let's just format print line and we'll say color is red and then we want to do an else if to check for something else specific so else if we'll say color is equal to blue then let's copy this paste that in and let's say color is blue oops not blur and then just do an else so if it doesn't match either of these then let's format we'll say color is not blue or red okay so we'll run this and we get color as red change it to blue color is blue change it to green and we get color is not blue or red all right very simple so let's take a look at a switch and we'll use the same example I'll even use this color variable so we'll say switch and then what we want to test which is the color and then we want to present cases so we'll say case red okay what do we want to do will print out color is red we want to stay within these parentheses these curly braces for the switch and put another case of blue and let's say color is blue and then we want a default okay because if it's not red or blue then let's print out color is not not blue or red all right so let's try it out run it and we get the same thing color is not blue or red if I were to change the color here to red and save and run we get color is red for both the if/else and the switch okay so I think that's gonna be it for conditionals and of course you can do like you know and and or whoops or stuff like that as well if you want more than one but yeah that's conditionals let's go ahead and create a new folder here this is gonna be oh seven and this is actually gonna be loops okay so loops and let's say main dot go okay and let's copy that alright so let's see we're gonna do a we're gonna do a couple for loops there's basically out like a long method and a short method so let's do the long method and we're gonna just create a variable called I set it to one and we're gonna say four and then our condition which is let's say if I is less than or equal to 10 then we want to fmt print line and let's just print I and then we want to increment by one so we'll say I equals I plus one and let's run this so we'll go into what is it oh seven loops and let's run the go file and we get one we prints out one through ten and I know a lot of you guys are probably yelling at the screen because instead of doing that an easier way is to do I plus plus okay so that'll do the same thing it just adds one so for each iteration one gets added now we can do a short method as well which resembles most other languages that have four loops so we can say four and then put the assignment in here so so four oops I equals one semicolon then we put the conditional if it's less than or equal to ten and semicolon and then do the increment and then simply print out actually let's do a dot print F and we'll do we'll just put in like the text number and then let's do what is it so we want a base ten integer so it's a percent D and new line and then just pass in I okay so now when we run this we should see number one through number ten and there we go all right cool so let's do a quick fizzbuzz challenge here okay so if you don't know what fizzbuzz is it's a common interview question for you know many different languages and basically they won't you to loop through a hundred so have it print out with whatever one through a hundred and for every multiple of three it should output the word fizz for every multiple of five it should output buzz and for every multiple of three and five it should output fizzbuzz okay so I know I know a lot of you guys probably know the answer to this but just to show you how to do it and go which is basically the same in every language will say for I set that to one let's say I is less than or equal to a hundred and then I plus plus all right now the way the solution that that I always use which is the most popular is the first check to see if it's divisible by 15 because if it's divisible by 15 its divisible by three and five all right so we just want to do an if statement and we'll say if I and we want to use the modulus operator a modulo whatever I don't some people say modulo some say modulus but basically this is used to find a remainder okay so if we say if I modulus fifteen if that's equal to zero that means there's no remainder so that means that this is divisible by 15 okay so if that's true then we want to output fizzbuzz so let's say print line and let's output fizzbuzz okay then we want to do an else if so now we want to check to see if it's divisible by three so we can simply say if I modulus three is equal to zero then let's actually just copy this then let's just output fizz okay if it's divisible by three and then let's do another else if and we'll say if I modulus five is equal to zero will print out just buzz and then else if it's not divisible by any of those then we just want to print out the actual number which is just I okay so save that and let's try it let's see if I pass the interview all right so we'll go up to one okay so one two fizz okay so remember it's divisible by 3 we get fizz for five buzz six is fizz that's divisible by three nine fizz divisible by three 10 divisible by five fifteen fizzbuzz is because it's divisible by both and then 30 should be the next one so fizzbuzz for 30 45 should be the next one fizz abuzz for 45 we have 64 the next one so that looks like that works okay so if you guys are going if you're new when you're finding a job remember this and it's very similar in in most languages all right so that's going to be it for loops now let's go into what's next Maps so I'm gonna say 0 8 underscore maps and maps are key value pairs okay so let's say main go all right and we'll copy that paste that in let's clear this up and CD into the right directory okay so like I said a map is is a key value pair so let's go ahead and define a map so I'm gonna call this emails and what I want this to be isn't it is a map of names will do first names for the key and then emails for the value so let's say emails equals and when we do it this way we want to say make and then pass in map and then a set of brackets and this first data type is for the key okay so the key will be a string and then we want to do the value the datatype for the value which will also be a string okay so that defines a map now let's say a sign oops sign will say sign key values so we'll say emails and let's say Bob you probably do first and last if this was a real-life situation but that's fine let's say Bob gmail.com say Sharon I don't know where these names come from in my mind Sharon all right so that gives us two two key value pairs now let's let's see what happens if we output these so we'll say print line and then let's say emails and we'll run this go run main go and there we go so it shows that it's a map and it shows us the values okay so if we wanted to show like just one like let's say we wanted Bob's email we could do emails and then put in the key Bob this might be a bad example but you guys get the point you probably have like an ID for for the key because there could be more than one Bob so this is kind of a stupid example but it gives you the point so yeah so it outputs Bob at gmail okay and you can use the Len command for this as well if you want to see the number like if you want to do or the length of the the map like how many is in it so if we do that we get to all right now actually let me show you how to delete one I'm gonna add one more here so let's say Mike all right so I want to delete one so let's print out emails and then let's see we'll go down here and we'll delete one so we'll say delete we just wanted to do delete and then pass in emails and then the second parameter is the key so let's delete Bob and then we'll just go ahead and print the emails so I'll copy this yeah so we'll print it out so at the end we should see no Bob so let's run it and right here map we just have Sharon and Mike okay all right now there's we can also assign the key value pairs when we declare it like this if we want so what I'll do is I guess yeah we'll just comment these out I guess just comment out that and that and let's say declare map and add key values so we can say like emails : equals and we this time we don't want to use make we just want to simply say map say string string for the value and then we can do curly braces and we can put in our stuff so let's say Bob Sheeran I'll just do two alright so let's save this and let's see if this works okay so what we're printing out the emails which is this right here then we get to because we're printing out the lengths we get Bob and then at the end we just get Sharon because remember we delete Bob so you can do it either way you can can just you can declare or define the map and then add values and of course we could add values to this if we want to add Mike after that we could put him right here and now run it and you'll see that Mike is in there okay so those are maps now let's move on to range I probably should have did range well no that wouldn't make sense because range is used to loop through things like maps and arrays and stuff like that so let's say let's create a folder called zero nine underscore range okay and then we're gonna create main go let's copy this and let me just CD into range alright so like I said this is used to to loop through arrays map slices so let's create an array of will say IDs so we'll say actually it'll be a slice so I'll do just blank brackets here and then let's say int we'll just put some random numbers in here all right so we have our IDs so let's go ahead and let's loop through IDs using range so we're gonna say for and it is a for loop but we're going to do this differently so we're gonna say for I and then put a comma here and ID is what we want to use within the loop for each one okay for each iteration and then we're gonna do equals range and then IDs because IDs is is what we're looping through it's this right here alright and then in here let's do a FM t dot print F and what I'm gonna do here is do a digit amount of digit it's a base 10 int and then we'll say - ID and then let's do % D again and put a new line and then as far as what we want here we want the index and then we want the ID all right so let's see what that gives us okay so what we're seeing here is the index that's the first thing because that's what is you know we have is the first parameter over here so that's what's going to be put here second one is going to be filled with the ID which is each one of these so each iteration it's gonna put out the ID okay so let's sum and just to show you real quick if we weren't using the index in here a lot of times you won't be like if I just wanted to do actually let me copy this let's say not using index okay so if I take this out we'll just say ID : d and just use the ID here you'll see that I'm getting an error and it says I declared and not used so just like when you define a variable the normal way if you do it here you still get that error so what you would do is you would just put an underscore okay if you're not going to use that you just put an underscore and then it should work fine so let's run it and you'll see the second batch here is just ID and then the number okay and you'll see this a lot in go so let's actually let's uh let's add all the IDS together and ID's together range is used for a whole bunch of stuff so let's say some and we're gonna set that to zero and then we're gonna do a four I'm not going to use the index I'm gonna do an underscore okay so for ID equals range IDs and then we can just take that sum and we can say plus equals ID which is like saying sum equals sum plus ID it's just a shorthand way of doing it it's available in most languages and then let's format our languages I deal with so print line and then let's say some and some alright so this should give us all of the IDs added together so let's clear this out and run it okay we get our loops and stuff at the end here though you see some 199 okay so it's just looping through and then adding all of these together alright so let's say let's use uh let's use a map because up to this point we've only used slices or arrays and I call I call slices arrays a lot I mean they're the same thing really they're just not you know finite whatever so let's go down here and let's say range with map so we'll use the same example the emails I'll actually just grab that so we'll take this right here and let's put that in and then let's say for now here we have key value pairs it's not like an array where it's just a single value so we want to say K V and you could use whatever you want I'm just using K V and then equals range emails okay and then we can say format dot print line and I'm going to use let's see if we go back to Chrome and we go to that format or you'll see that percent s is the uninterpreted bytes of the string or slice so I'm actually gonna use that it's kind of a weird description but I'm gonna do percent s : and then another percent s because I want it to be the the key and the value and these are just like placeholders I could do print line you know kV but you know concatenate them but I think this is a neater way so let's do percent s just make sure we add our new line and then we're gonna put in the key for the first one and then the value for the next so we should get the name : email what's going on here print line call as possible oh I'm sorry this should be printf all right so let's run this and go down to the bottom and you can see now we have our keys : and then the email so that's what we did here okay and if you just want to get the key you can do that as well let's see if we say four K equals and then do range emails and let's just do an FM t dot print line and I'll do a concatenation we'll just say the key actually their names I'll say name and then just concatenate the key okay so now we should just get the names we run this down here at the bottom you'll see named Bob named Sharon wait a minute oh okay I thought I had more than more than that alright so I think that's gonna do it for range now what I want to talk about is our pointers okay so let's see we're gonna create a new folder called ten underscore pointers [Music] and let's create a file called main dot go gonna copy our hello world copy it no why the hell isn't it copying all right so let's see now a pointer basically allows you to point to the like the the memory address or the location of a value okay that's in a variable and the way that we do this is I'm just gonna give you an example here if I say a is equal to five okay and then I'll say B is equal to ampersand five I'm sorry ampersand a so what this is doing is it's a site it's assigning B to a pointer of a so let's go down and let's print these both of these out and see what we get so if we say fmt dot print line and let's do a and B and see what we get so we're gonna CD into ten okay so we want to run the file and this is what we get so a is printing out as five B is printing out as this thing okay and some of you may know what this is some of you may not it's a memory address it's basically where this value is stored on the system okay or in memory I should say and this is what they look like zero X and then a bunch of numbers but yeah that's that's the memory address if we want to check the type let's go ahead and do that let's say format dot print F and remember for the type we use percent uppercase T we want to do a new line and then let's put in let's look at a so if we do a we get int okay which is fine that's to be expected if we look at B we get star int okay so the star represents a pointer so an int and us a pointer hint these are two different data types okay these are not the same now if you want to read the value from the the memory address you can use an asterisk so let's go down here and I'll just use to read Val from address so we'll say format dot print line and let's say asterisk B if we just do B it'll give us the address but if we want the value we can check it out like that so let's run it and you'll see we get five okay if we do just B we get the memory address okay so use that and if this is the same thing as doing star and then ampersand a because remember B is set to ampersand pay so this is going to be the same thing five okay now we can actually change the value with the pointer if we want so let's say changeval with pointer so I could say star B or asterisk B equals so it's set to five let's set it to ten and then let's go ahead and format dot print line and I want to now check the value of a because remember B is set to the memory address or the pointer of a okay or it is a pointer so let's save that and let's run it and now down here you'll see that a is equal to 10 so we didn't explicitly change a to 10 we set B to the memory address or we set a pointer to that value and then we changed it with the asterisk and that actually changed the value of 5 and I mean this is not a very practical example because I want to keep it simple but the reason you would use pointers is because you might have to pass a lot of data stored at an address and if you if you choose to pass the address instead of the data itself it can increase performance and it can be you know you can make things faster they're also used to just change values at specific locations you know everything everything in go is passed by value you have other language where you can like pass by reference and stuff like that everything in go is passed by value all right and I know that that's not the best explanation on pointers but that's all I have for this course all right so next we're going to take a look at closures so let's create another folder called eleven underscore closures and let's create a file in here called main go all right so I'm gonna CD into that alright so so go can support anonymous functions which can form closures we can define a function in line without having to name it so let's copy this the closures is one of those things that I just hate explaining so I'm just going to kind of show you the syntax we're gonna create a function called adder that we can actually set a variable to and then we can use that we're going to put it inside a loop and have have it add all the iterations together okay so let's go ahead and get rid of this and let's create a new function up here call it adder and what I'm gonna do here is set another function so an anonymous function with no name which will take in an INT and return an INT and then we're gonna set some equal to zero okay so it'll start at zero and then let's return a function that takes in let's say X which is an int and we'll return an int and then we'll take that sum and we'll just add whatever is passed in so we'll do plus equals for shorthand we'll just add whatever is passed in and return the sum okay so very simple function and then down here what I want to do is do a loop where you go one through ten and then just kind of add add them all so let's define some here and what we can do is we can now assign it to the outer function like that all right and then it will create a simple for loop so let's say for and we'll say I is going to be equal to zero and then we want to say as long as I is less than 10 and we want to increment and then inside here we just want to do a format dot print or print line and we want to use that sum now remember we set some to two adder so we can actually pass in a value here which is just going to be the current index of the the iteration all right so it should just kind of keep adding them so let's save that and let's run this so say go run main dot go so it starts the iteration at 0 and then 1 and then it goes 1 plus 2 is 3 3 plus 3 is 6 6 plus 4 is 10 10 plus 5 is 15 so it just keeps adding the next number in the loop ok so I mean it's not the most practical example but I think it's a it's a relatively simple one but yeah and then like I said I'm not very good at explaining I'm not a very very good at explaining functional programming in general JavaScript or any other language that's why I don't do a lot of it so let's see we're gonna move on to strux okay now structs are of very important part of go I would say one of the most important parts it's based they're basically like classes there's no classes and go it's basically like you assign you create a struct like let's say for a person and then they can have different properties like name age email whatever and you can have different methods that are associated with that struct and there's two different types of methods there's pointer reference I'm sorry pointer receivers and value receivers so I'm gonna give you an example of all that stuff so let's create a new folder called twelve Struck's and in here let's create a file called main go get rid of that paste that in and down here I'm just gonna CD in two strokes alright so let's see the first thing we'll do is define a struct so I'm going to go above the main function and let's say define person struct okay the way that we do this is we say type and then name it I'm going to call it person and then struct okay then we want to define the properties we want so I'm going to say first name which is going to be a string last name string what else did I have we have a city let's do gender and let's do age okay so that's a struct down here let's initialize a person using this struct okay so I'll say an it person using struct in the way that we do that is create a variable called person one and we'll set it equal to person so this looks very familiar to like you know JavaScript classes PHP Python all that stuff so we just want to attach all of our fields so let's actually we want to put in some curly braces here and we can do the property name and then the value so for instance let's do Samantha lastname what else we got city get gender or gender I'm gonna do MRF I don't mean to offend anybody I don't I don't know any other genders I apologize so age is gonna be an int I don't mean to sound disrespectful let's do 25 all right so let's do fmt dot print line person 1 and let's take a look so I'll run this and we get Samantha Smith Boston female 25 okay so we created a person from our person struck now you can also assign without using the the property name like this so just to give you an example let's say alternative so we can just take these away that and that should still work so if I comment this out and we run this we should get the same thing okay now this is more descriptive in this case because we have just hard-coded values but a lot of times you'll have variables like name you know so in that case you don't really need to do this because it's descriptive what this variable is but in this case it's just like hard coded text so I want to keep it this way the first the first way so let's uncomment that alright and then if we want to get a single field we can say it actually need to put a comment for every single thing let's do format dot print line person one dot save first-name if we run that we get Samantha okay so you can access it you can also change them so person one dot age let's just do age plus plus or you could do equal something but now if we print the person let's just move let's move this down here so we can see all the fields after we change the age and you'll see now we get 26 alright so now let's talk about methods so we have two kinds of methods value receivers and pointer receivers and value receivers are four methods that just do like calculations and stuff they don't actually change anything pointer receivers are for when you actually are changing something so let's do an example of a value receiver now our methods don't go inside the struct okay they don't go like in here like you might think like they would with a class we're gonna go outside of the struct okay and let's say we're just gonna do a greeting so let's say greeting method and this is a value receiver so to do this we're gonna say funk and before we rename the method or the function we need to put in an identifier so I'm going to use P you could put anything you want here and then person the name of the struct okay you could also you could do person if you want but I'm just gonna do P all right and then we want to give this a name I'm gonna call it gree and then we want to give it a return value which is gonna be a string okay so this is how we can create a method and all I'm gonna do here is return and we're just gonna do a greeting so we'll say hello my name is now the way that you access in this case of Persons let's say first name is we need to concatenate because we need to use a variable we would take P because that's what I used right here okay so I say P dot first name like that it's it's similar to this that this keyword in many other languages okay like if you were using es6 or PHP or something you would use this dot first name but here you actually you specify an identifier here okay if I were to put person here would be person dot first name so let's do that and let's concatenate a space and then we'll put a last name so P dot last name and and I am and let's put our age in here so or whoever's I mean this is we this can be used on any person you create will say I am P dot age alright so I'm gonna save this and you're gonna see that we have an error here so the reason for this is because we're I'm returning a string like this I can't have mismatched data types in here and age is an int okay so that's why it's giving me problems these are strings which is fine but I can't just simply put an INT in here so what I'm gonna do is change this to a string and I'm gonna bring in a separate package called string converter or STR Co and V is what it's actually called so I'm gonna go here and just go on a separate line here and let's say STR CO and V okay so we can bring that in and that string converter has a method let's say he's pissed er why isn't it showing up string converter dot and the method is a capital I Toa okay and we're just gonna wrap that okay now if I save you'll see the error goes away okay so that's one solution I mean there's a lot of different ways you could you could deal with this but that's that's a value receiver so let's actually call that method so we'll go down here I'm going to just comment everything else out and let's do a print line and we'll take person one Samantha and we'll call greet you can see it's actually showing up here so we'll say person one Gris all right now let's clear this up and run it we get hello my name is Samantha Smith and I am 25 alright so that is just a value receiver and the reason it's a value receiver because we didn't change anything here all we did was was take take some of the values that were passed in here and just created an output a greeting so let's create a simple pointer receiver and we'll change something so let's say we want a function or a method I should say to change the age they could say they has a birthday we'll call it okay so let's say as birthday method and this is a point to receiver and it's a point to receiver because we're gonna change something so to do this we want to say funk and it's the same idea we want to give it a specifier here and then since it's a point to receiver we need to use an asterisk okay so we want to say asterisk person which is the name of the struct and then let's do has birthday and this isn't returning anything it's just changing something so we don't need to put a return value and all we're gonna do here is actually what we did below where we take the age and we can access that with pH plus plus okay so let's save that and let's go down here and let's see the greeting actually has the age in it so I'll just go right before it and I'll say person one dot has birthday all right so now when we run this the age should increase by one and there we go I am 26 if they had another birthday or a couple birthdays 29 all right so that's a value receiver so let's do let's do another one let's do I get married okay and the idea is if it's a female then we're gonna change the last name and I hope nobody takes offense to this I know that sometimes they don't change the last name sometimes the male changes last name we're just going with with tradition here okay I gotta be gonna be careful these days because people are so sensitive but let's say it gets married or get married which is a pointer receiver so funk okay we're using the person concha construct person struct and it's a point to receiver so we want to make sure we put the asterisk and then person and let's say get married okay now this get married is going to take in a spouse last name which is going to be a string okay now what I want to do is test to see if this person is male or female so I'm going to say if P dot gender is equal to M then I'm simply going to just return and then else so else then we're going to change P dot last name and we're going to set that to the spouse last name that's passed in alright so let's try this out now we know Samantha is female so let's go down here and let's say person 1 dot get married and we'll pass in let's say Williams which is a spouse name so now let's go ahead and run the Greek and we get hello my name is Samantha Williams and I am 26 all right so let's create another person actually no we'll do instead of keeping this commented out let's make this person to change it to Bob Johnson New York male and 5:05 let's make them 30 all right so now what we'll do is we'll run person to dot get married just no semicolons it's by JavaScript coming out my PHP so here let's say they married someone with Thompson as the last name and let's do person to dot green see what we get alright so we get hello my name is Bob Johnson and I am thirty so he did get married to someone with the name of Thompson but where he's a male he didn't change his last name alright now there's one other thing I want to show you if you want to shorten this up a little bit if you have a lot of different properties here what you could do is take all the same data types and put them on the same line so first name last name city gender and then just say string and then age int okay and then you could I'm just gonna comment these out just so you guys have it and save it vs code if you use prettier we'll push this the hint over to the side so these are lined up but you could also do that so if I run the go here which it still works alright so just a shorter way to define this stuff alright so now we're gonna move on to what are we moving on to interfaces which are basically data types that represent a set of method structures not method structures methods signatures for Struck's okay so you can define an interface for instance we're going to have an interface called shape that has a method called area and then we can use that interface on struct called rectangle and a struct called circle because we want to use that area method in both of those okay so let's create a folder called thirteen underscore interfaces and the file called main go okay so let's grab that alright so we're going to use the math package so we want to wrap this in parentheses and bring in math and we'll put that on a separate line all right and then above the main function we're going to define our interface which is going to be shape so we want to say type shape and interface okay and then you put all your the methods that you want this interface to implement inside here we're just gonna have one called area which returns a float 64 okay and now we can use this interface with different strokes so we're gonna have two strokes one it's gonna be circles so I'm going to say type circle struct and let's give it some properties alev Y and radius and these will all be float64 okay then we'll create a type of rectangle which will be a struct and this is gonna have a width height which will be float64 all right so very simple now we want to do is create the area method for both of these struts for both the circle and the rectangle all right so let's say funk and this is gonna be a method for the circle so let's say C circle and this is a value pointer because we're just doing a calculation we're not changing anything so we don't need the asterisk right here because it's not a pointer receiver or a value set value receiver all the terminology is confusing so let's say funk see circle and it's gonna be called area it's gonna return a float 64 not 65 64 and the calculation here is we want to return we're going to take PI so math dot PI we're gonna times C dot radius and then times C dot radius okay so that's that and then we're gonna have an area method for the rectangle so let's change this to R so the rectangle area that's the same and then for the calculation here we're just gonna say our dot width times r dot height okay and then we want one more function called get area so func get area and this is gonna take in our interface of shape and say float64 and then all this is gonna do is simply return our shape dot area method all right so now we should be able to initialize a circle and a rectangle and then simply pass it in to get area to get the area so let's say circle equals what I keep doing that equals a circle and then we want to pass in a vet some values so X is going to be 0 y is going to be 0 and radius is going to be 5 okay and then rectangle it's going to equal rectangle and we're gonna pass in width so width will be ten and let's say height will be five spread these out a little okay so now we have our circle and rectangle now I just want to wrap these and get area and print it out so let's say format dot we'll do print F and I'll say circle oops area and we're going to use a percent F and then a new line and then we're just going to pass in here the circle all right and then we'll do the same thing for the rectangle and that's it let's try it so I'm going to CD into what is it thirteen interfaces and let's run the file and there we go circle area wait a minute the hell that's not right oh I didn't know I'm just printing out these I didn't wrap it in get area so we want to wrap this in the get area all right so now that should get the area there we go so circle area for this is going to be seventy 8.5 blah blah blah rectangle area is going to be fifty okay so that is an example of an interface so basically just a list of methods that you can implement on different strokes I don't know what these are should have comment or be unexplored these are just like tips linting or whatever alright so the last thing we're going to look at is web so let's close this up and let's create one more folder called 14 underscore web and I'm going to create a file called main go okay I'm also going to CD into that oops alright so let's bring over our hello world and we're gonna bring in another package to work with the web to deal with like HTTP requests and stuff like that and that's called net / HTTP okay I want to put that on a separate line and then what we'll do is in here we need to take that HTTP object and call handle func okay now this is kind of like a router it takes in a row so I'll do slash and then you can put in a function to deal with that row which I'm going to say index okay and then up here I'm gonna say func index and this takes in two special parameters that have to do with that HTTP module so it's going to take into W and it has a special type of HTTP dot response writer oops so response writer and then it takes in an R which is gonna have a pointer method of HTTP dot request okay and then in here if we want to output something to the browser we can use format dot F print F okay which takes in a formatting receiver which is going to be W and then the next parameter is whatever we want to output so we'll just say hello world all right now let's save that down here in order for this to work in order for us to basically serve we need to take HTTP and call listen and serve okay it's kind of like with Express like app dot listen okay it takes in a port so we're going to say : let's say 3000 and then the second parameter is gonna be just nil which is just like none like nothing and that should actually run a web server so let's try it out okay so you see how it's kind of like hanging here actually what I can do is I'll control yeah ctrl C to stop I'm gonna put just a little message in here so I'll say fmt dot print line and let's say server starting just so it doesn't just hang there like that and then we'll run it we get server starting and now let's go to our browser and we should be able to go to localhost colon 3000 and there it is hello world ok and we can actually put HTML in here if we want so if I say h1 and save that Oh probably think we have to restart the server though let's reload yeah so we just have to stop this and start it back up and now if we reload we get hello world okay if you wanted to do another route we could copy this down we could say this one this will be in a boat function and we want it to be slash about and I could just copy this and call it a boat and then we'll change the text and save let's restart server down here and check it out so that still works and if we go to slash about there we go alright and there's like I mean go is really good to build like rest api's and stuff like that micro-services but you can also use like template engines template languages whatever if you want there's a lot that you can do with the web and I really want to start getting into it and that's the point of this whole course is to prepare you guys for that so you know at least the basics how to create you know variables and arrays and all that stuff to deal with you know maps and Struck's which are very very important so that's it guys I know this was a freaking long video I think this was the longest video I've ever made so you know if you made it to the end thank you you must really like my content if you were willing to sit through all of that but thanks thanks for watching guys I really appreciate it if you liked the video please leave a like and I will see you next time
Info
Channel: Traversy Media
Views: 323,059
Rating: 4.9516068 out of 5
Keywords: go, go programming, golang, go lang, go programming language, golang tutorial
Id: SqrbIlUwR0U
Channel Id: undefined
Length: 98min 41sec (5921 seconds)
Published: Tue Sep 18 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.