Golang Tutorial : Go Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to my go lang full course video tutorial you guys asked me to cover just about everything with golang in this video i am going to do it this is the longest video i've ever made this is just a brief sampling of everything that will be taught in the description underneath you'll find all the code it's on github for free and also there are no ads for the rest of this video and you'll be able to jump around and to all the individual pieces you want to learn or you can go on github and get all the code so why use golang or why even learn it it is a fantastic general purpose language that is very easy to understand and write good code with right out of the box yeah there might be languages that are faster if you program them perfectly but with go you will very often find that you can write extremely fast code without having to worry about doing everything perfect which is great it's a great back end language it excels at concurrency it has many built-in features strong typing handles memory for you easily fast compile times and cross-platform and i have a lot to do so let's get into it all right so i'm going to be using visual studio code for this and the name of our file is going to be hello go.go but we're going to be making a whole bunch of different files and there's no ads for the rest of this video no sponsorships or anything just so you can focus 100 on education and so you just want to create a directory somewhere and name a file hello go.go or you can name it whatever you want and we're going to jump in here and the installation part is at the very end of the video so as not to distract first thing we're going to do is i'm going to get into packages and modules and all this stuff later on in the video but for right now i'm going to keep it very straightforward now a package is just a collection of code and we can define what package we want our code to belong to with just by just saying package main for example we're going to be using other different names but whenever we use main that means that this is code that is going to be run in our terminal so now what we need to do is go and import some packages that we are going to use and i'm going to just import all my packages all at one time so i'm going to go import and i'm going to say format like this now very often of course we're going to want to be able to print out to our terminal and you're going to use a command called print line or print there's a whole bunch of them and i am going to abbreviate this so that i don't have to every single time type in format dot print well it didn't pop up with a shortcut oh that's because it didn't get that yet okay instead of typing all that out what i'm going to do instead is i'm going to create an alias and you can do this for everything just don't get crazy so i'm going to use p l as my alias for print line so anytime you see print line like that or pl you're going to know that it refers to that and we do not have a semicolons but you can use them if you really want to a comment is just going to be two slashes like that or you can have multi-line comments so let's do a multi-line like this and like that and you can jump through uh to all the different parts of the video if you want to only want to learn a specific part of course so what do we want to talk about first well the most important thing for us to get an understanding of is our main function so whenever a go program executes it specifically looks for a function called main and that's where it begins its execution and you're going to define all your functions by typing in func and main like well not everyone who's going to be called maine of course and we're going to define our main function now if we just simply want to come in here and do our traditional hello world maybe we instead want to come in and say something like hello go we can do that like that and i have code runner running inside of here you can just come up here and run this and you're going to see hello go pops up right there all right very simple stuff now let's say that we want to instead get some user input and do something a little bit more creative well inside of here i'm going to say something like what is your name and i am then going to go and get some user input so i'm going to call this reader and i'm going to go buff i o dot new reader and what this does is it sets up a buffered reader that gets text specifically from our keyboard if we type in os dot standard i n like that and you can see there's little errors here that means we need to import buff io so let's just go buff io like that and then let's also go o s like that and we can get these in here and just paste this down here and just down here all right so there we go now we got all those set up now what i want to do and the error here is saying hey you didn't you declared this but you didn't use it if you were aware of rust there are some similarities in regards to what is and not considered best practices with go all right so we got that all set up so what i want to do now is i am going to say that i'm going to store this input inside of name but i also want to protect that some type of error occurring and i'm going to do that by following the comma up here with an error that could potentially occur and then i want to handle it so i'm just going to then call our reader and go read string and i am then going to say that i'm going to read up until the point in which we get a new line or the user inside of the terminal hits the enter key now if i didn't want to get the error i could just go and use what is called the blank identifier but that's considered a bad practice so instead i am going to handle my error instead of ignoring it so i'm going to say if there was or if the error is equal to nil well in that situation i know i did not get an error more on the specifics of why i know that but just understand that if nil is passed back that means there was no error i'm then going to follow that up with the name that they gave me and otherwise i'm going to say else i'm going to go log.fatal and i'm going to display my error message and handle it again normally whenever you see those little squiggles that just means that we need to go and import a new library all right so we have that all set up let's run it and test it out so what is your name and it's derek and there you go hello derek all right and so we move on from here next thing i want to talk about are variables and normally with a variable you're going to define where you're going to put var like that every time and then you're going to say whatever the variable's name is followed with the type and the name must begin with a letter and then it was allowed to contain letters or numbers and one thing to know is if a variable function or type starts with a capital letter it is considered exported and can be accessed outside of the package and otherwise if it's lowercase that means that we're not going to export it more on that later don't worry about knowing the specifics of that so let's come in here and let's create a variable and i'm just going to go v name like that and i'm defining what data type it is which is a string and then i am going to just type in my name like that and you can see everything is set up here and something else you may notice is camelcase is used for our naming conventions inside of go let's go and create let's say you wanted to create multiple different variables go v1 and v2 is equal to 1.2 and 3.4 something else that's legal to do those little squiggles just mean that i defined but did not use those variables everything else is cool all right let's go and create another one let's go v3 and define this as hello this is a situation in which we are asking our compiler to go and figure out the type for us which it will gladly do and variables are mutable by default that means values can change as long as the data type is the same so you can't go and create a variable called a string and then store a string inside of it and then change it to an integer doesn't work that way another thing you could do is get rid of the var part and say something like vr equal to 2.4 and again it's just going to automatically go and define our data type and after you're declaring variables to assign values to them you're always going to use an equal sign thereafter so this is okay whenever we are initially assigning a value but later on if we want to assign a value to it we have to go equal to 5.4 or something like that and you'll see if we come in here and use this instead that that is going to give us an error i think that's enough on naming conventions of variables now let's go and talk about data types now the core data types are going to be ins float 64s bulls strings and runes are there other ones yes there are we're not going to talk about those quite yet though they're going to have default values n sub zero floats of 0.0 booleans are going to be false of course booleans have true or false values and strings and ruins are just going to be empty values with nothing inside of them let's go in we can say something like we want to go and check out the different types so we can say type of like that and we want to find out what 25 is inside of here you can see we are getting an error right here and that just means we need to come up here and throw this in there again so whoops just so reflect and we can use that as well and you can see that the unused libraries disappeared and we'll go and throw some other different types of data inside of here so let's do 3.14 and we'll do true and we will do hello and you can store all kinds of weird things inside of here um let's get rid of that type in hello instead you can even go and do some sort of like graphic if you would like let's just throw something silly inside of here um like that for example okay and we're going to save this and i'm going to go cls clear that and run it and you're going to see all the different data types that we have under these circumstances okay so basics of data types more more is coming and you're going to learn more about different data types as we continue but now i want to talk about casting because it's very important now to cast all you're going to need to do is let's just go and create a variable like this let's call this cv1 and go and allow the compiler to find the data type for this and we'll go c v2 so zv2 and equal to now if we want to go and get this float and convert it into an integer all you need to do is put whatever type you want it to become on the outside of the parentheses and that's what it'll do it'll automatically go and convert that for you and we come in here and go cv2 like that and run it and you're going to see that this is just going to knock off the decimal part of our number now let's come in and define cv3 and equal to and we'll just throw in a great big number so 5 like that and let's say that we want to go and convert this let's make it a string so change that into a string and we want to convert this string into an integer well we can go and say we want to store this in cv4 and we're going to protect against any errors that we might get and we're going to go string convert this is going to be another library we need so string convert like that and then we follow that up with ascii two integers what this stands for ask e2 integer and cv3 like that and then we can say pl and cv4 instead of handling this error let's go and just pass this out inside of here and i'll go reflect and type of anc v4 and jump up inside of here and go reflect again and go and run this guy as well and you'll see that nil came back in that circumstance with our error i just wanted to show you that as a change of pace different thing and you can also see we successfully we were able to convert our string type into an integer type as you can see right there now if you would want to convert an int into a string let's just call this cv5 all the code is available on github by the way i guess i probably should have said that earlier and you can go and download it for free and there's a link in the description so cv6 is going to be and we'll just get rid of this all together and we will go i t o a and this is integer to ascii that's what that means and this is going to be changed into 5 and then we can come in here and just print this out so like this and c v 6 like that and run it and you can see it comes back right there uh let's say we would want to convert a string to a float how would we do that let's go change this into cv7 and get rid of this part change this to 3.14 and let's go get rid of this all together and what i want to do is i'm going to handle potential error i'm going to say if we'll get more into if statements later on but if you're learning go right now go is normally not a first language so i think that chances are you've probably seen an if statement before so we're going to go parse float i'm going to get rid of this so i have more screen real estate to work with maybe move that over a little bit so i'll say parse float and cv7 64 and i can go error equal to nil so i'm gonna do the error handling right here and i'm gonna go pl c v eight so there's another way of handling an error if you would like multiple ways and there are multiple ways and you can see here is our float and we can also do some formatted printing if you wanted to go and actually convert a uh or put a float and convert it into a string we could do something like cv9 like this and call format dot s print f and then if you're using floats you're going to use a percent sign in f i'll get more into these printf types of formatted printing later on in the video actually pretty sure pretty soon but f just stands for float d is going to be used for integers and i'll get into the other ones as we go but if you wanted to convert from float to string that is exactly how you do it and the value would be stored inside of this variable right here all right so that is a whole bunch about casting and now what i'd like to do is talk about the if conditional now i'm assuming you have a basic concept of how if statements work and so i'm not going to cover them in massive detail i'm just going to get into the specifics that are used with go all right you're going to have the conditional operators you're always using greater than less than greater than or equal to so forth and so on and this is not equal and of course this is equal we're also going to have logical operators so this is going to be and or or not okay so those are going to be the operators we're going to use whenever we are performing different conditional logic now what i'd like to do is i would like to receive an age and i would then like to decide if a birthday is going to be important or not based off the age i'm going to say anyone who is 18 or under is going to have an important birthday and anyone who is 21 or 50 that birthday is important and then anybody who is over the age of 65 their birthday is important but everybody else your birthday isn't important okay so i'm just going to come in here like this and how we would structure this is i'm going to say if i age is greater than or equal to 1. now if i want to have multiple conditions that have to be true for us to be able to proceed i'm going to put an and sign inside of there and i'm going to say less than or equal to 18. okay and what i'm going to do here i'm just going to say that this is an important birthday and print it on the screen so important birthday there we are and i'm going to copy this because i will need it what else well we can stack our conditions so i could say if this wasn't true then i'm going to say if the age is equal to 21 or the age is equal to 50. well i'm going to say those are important birthdays also so i'm going to paste that in there and then i'm going to stack another one on i'm going to say else if the age is greater than or equal to 65 well then that's an important birthday and else this is going to be the default if none of these none of the other ones are true i'm going to say that anyone who is not in any of these other conditions not an important birthday all right sorry okay and let's run that and if we do it's gonna come back important birthday why because the person was eight years old now the not conditional is just going to turn the booleans into their opposite value now a boolean of course is either going to have a true or a false value that is it those are the values for bull so you would use the not normally in conditionals very often in loops and things like that you're just going to say let's use not on the value for true and if you are using print line you do not need to put a space here there's a space automatically added at the end just specifics if you're seeing weird stuff well that's why it's weird and we can run this and of course it's going to come back as false alright so there you go there is a little bit of conditional logic more is coming now let's talk about strings all right so let's go and create a string and i'm just going to allow the compiler to figure out that this is a string by using colon equal and strings are just going to be arrays of bytes which are going to actually look like this which we'll get more into later on but that is basically what a string is now let's say that i would like to come in here and get the letter a and replace it with the word another well to do that we are going to use what's called a replacer equal to and i'm going to say strings it's going to give me an error message that's because i don't have the strings library so strings like that and i'm going to follow this up with new replacer and i'm going to say hey anywhere you see whoops this is going to be strings dot anywhere you see an a what i'd like you to do is to place another there instead all right and that is going to work for us and then what we can do is we can use this replacer to come in and replace those values so we'll say replacer dot replace and sv one and then we could go and output this if we would like so s v two like that let's go and do something some a couple more so we don't constantly run here and we get some more stuff all right let's say you would like to get the length for a string you're going to use len almost every single time everywhere you want to get the length of anything let's say we would like to come in and find out if a string contains another string we can say contains another for example so we'll say does this string contain the word another and then we're just going to follow that up with strings dot contains and we'll pass in the string we want to search for and then we'll follow that up with what we are specifically searching for and open this up a little bit there we are let's go and do a couple more here let's say that we would like to find out or we would like to get the first index match for a letter o so we'll say p l like that and i'm going to say o index just to keep it nice and short and i can say strings dot index s v two and we're looking for the letter o what else can we do oops accidentally stole that parentheses let's say we would like to replace all matches of uh the letter o with uh zero just for the heck of it so we'll do pl and i'm gonna say replace so we can see what this looks like and i'm gonna say strings dot replace typing a little fast here the string we want to work with is this one one we've been playing around with and we want to search for os and we want to replace them with zeros and i'm going to type in negative one and this is going to search everywhere and if i have had like a two or something like that it's going to replace the first two matches so that's what that last part is but i'm going to replace everything by putting negative 1 inside of there let's say that i would like to remove white space characters i'm going to create a new string let's go sv3 like this equal to and the white spaces i'm going to use are going to be new lines i'm just going to go some words like that and then maybe throw another new line you're going to see more with new lines it just goes and causes the terminal to jump to the next line and there's a couple different these are called escape sequences anytime you see a backslash with an n that that's an escape sequence and you also have things like tabs or let's say you would want to put double quotes inside of double quotes you know inside this string for example you could do so if you go and escape them just well you just do it one time and let's say you would like to do a backslash inside of a string you just go like that all right so there's those and i wanted to go a little distracted let's go and get rid of this white space you can go and save it back into the original string by going sv3 equal to remember whenever you do assign a string a second time you always use this regular assignment operator so i'm going to go and call strings and trim space and sv3 and it knows to get rid of all of the white white space that we have inside of there just make sure you give it the proper name uh let's say that i go and well let's just create something so i'm going to say that i want to split on a delimiter and a delimiter is just a common thing that is used in a string to separate the information so i'm going to say split um like that and i'm going to go strings dot split split and the string i'm going to be working with is a dash b dash c dash d and this would be the delimiter why it separates all the individual pieces of data and that's what a delimiter is and i'm going to define what the delimiter is and it's going to just get rid of all those delimiters and then just give me just the letters also we're going to be able to come in and let's say we'd like to convert something into lowercase or something so we'll go like this and lower and get our string so we'll go strings dot to lower and sv2 and we're going to do the same we can convert everything into upper case and this works or is very useful anyway very often whenever you're just analyzing strings and we'll do upper in this situation so up er and what else we're going to be doing more other different using other different string functions as the tutorial continues i just wanted to give you a rough overview of a whole bunch of them here and don't dwell on it too much let's say that we would like to look at a string and see if it begins with another string that's going to be strings dot has prefix that's the same as saying this is uppercase almost always these functions are uppercase why because they're exported from another package called strings and the only way you can use them is if they're uppercase if they were lowercase you wouldn't be able to access them it's just a rule inside of go that you'll get more acquainted with as we go and taco cat sounds like go has a lot of different rules but they're pretty easy once you play around the language for a while you're going to find that the rules actually make using gold go easier instead of harder which is sometimes true with other languages so and we'll just call this has suffix and this just means that it is going to end with a specific string so we'll say cat and i think that's it for now that's good overview of a lot of different string functions and there you go you can see that the length of our string is 12 does it contain the word another yes it comes back as true where's the first index where we get an o letter o that's the second you can see here we replaced all the o's with zeros you see here we split and created an array by getting rid of those delimiters we went used up a lowercase and uppercase prefix and is true for both of those all right so there's an overview of a whole host of different string functions and now i'd like to briefly talk about runes now in go characters are called ruins and ruins are unicodes that represent characters so let's go and create this string that we have right here we go a b c d e f g that's good and i'm going to say that i want to count the number of ruins so i'll say ruin count and to do so you go utf 8 and of course that means we need to come up here and go utf-8 utf-8 and then we will follow that up with ruin count in string and then i'm going to pass in my string that i want to go and count and let's say that i'm going to print out all of the ruins in the string using a for loop this is the structure of a for loop and it's just going to go 4 i in ruin val equals to range and i'll briefly explain what this means and basically what this is doing is it's just cycling through all of the letters inside of this string one by one and it is this is going to represent our index for each of those strings and this is going to represent our individual ruin value and if we type range in front of a string what it what that's saying is we want to get each of these letters one by one and do something with them okay and the for loop is just a looping structure just like it is in numerous other different languages if that didn't totally make sense i will be covering for loops in more detail as well as ranges in more detail so another thing i'd like to talk about is a printf function what this is is it's just formatted printing and it just makes it very easy for us to place our different variable values in multiple different places inside of a string so the very first thing i want to do is i want to print the index and now for this string that's going to be 0 for a b is going to be 1 and it's going to continue therefore forever all right so if i want an integer i want to place an integer in this final string that is then going to be displayed i'm going to put in percent sign d i'll do a whole bunch on printf and all the different codes but i'm going to give it the overview here now if i would then want a ruin unit code to print out i just go percent sign and i'm going to say the hash symbol and u and then finally if i would like to go and get the character to show up inside of there and just go percent sign c and whenever you're using printf you have to put in a new line to have that show up so there's our index and what's going to happen is this guy right here is going to go right here in what prints out on the screen and we're going to go rune value this guy right here is going to be printed where this is where that string is and then we will what else oh i'm going to go ruin value again however this time instead of a unicode that's going to print out it's just going to print out the character itself and if we go and run it we can go and oh utf-8 i thought i typed it in up here oh i got ahead of myself let's get rid of this like that and not like that and now let's run it and you can see how it prints out the unicode along with the character itself and the unicode you know representation the character and here we have our index all right so more on ruins as we continue but now i'd like to briefly talk about how we can work with times now you're going to find that it's very often very useful to be able to get time information and if you'd like to do that well first off you need to go get your time library and then inside of here let's say that i would want to get the current time at this moment so i say now and then i just go time like this and now and it's going to give me all sorts of time information so let's say that i would like to go and get my current year i can just go now year and it's going to give me that i'm just going to print all these out together i'm then going to say now and let's say i want to get the month do that and now dot day and let's go and get some more in information on the now that we're living in i can then go and get the hour so hour and seconds you're going to find is very useful for getting or generating random values let's say i want to get the minute and then finally the second get all of those guys and run it and you're going to see that it is 2022 august 5th and this is in military time that we have right here so this is going to be 804 with 18 seconds all right so brief amount of information about time and now what i want to do is i'm going to talk about some different math functions and we will use time once again to come in here and generate random values now i'm not going to waste your time here is everything uh here's some basic operators so if you want to add values and subtract multiply divide and this is going to give us the remainder of some type of opera of a division that's what that is all right and also you can do shorthand incrementing so for example if i had something like m and i wanted to add one to something called mnt i could do that well let's go m int like this is equal to one all right now let's say i wanted to add one to it well i could go m int is equal to m and plus 1 or i could go and get rid of this and put a plus sign like that and one those are exactly the same and then something else that is very useful is we're also just going to be able to go plus plus like that and you're going to be able to do this with all of your different operators that you would have um another thing let's just go and get rid of all this what's very very important is to understand precision whenever you are working with float values so let's go and just get an absolutely huge value like this okay so these are gigantic whoops let's go many many many floats and you're going to see that this is going to hold the precision whenever we're doing those calculations even though those are tons of of decimal places so that's very very important to understand i also said that i wanted to show you how to create random values i'm going to use time to do this again that's the reason why i covered time so early so we're just going to come in here like this and one thing that's important to understand is it's very useful to get a seed value for your random number generator that's based on seconds and it's actually based off of seconds since the date 1 1 1970. that is the number of seconds since january the 1st 1970 is what you're actually getting i'm going to call this seed sex like that and i'm gonna go and get the now time and then i'm gonna go unix like that and this returns the second since january the 1st 1970 and [Music] it as an integer and then you can use this as a seed value to generate random values so we'll go seed seconds there we go and if we want to generate a random value from 0 to 50. let's go random number like that we would call rand dot int n and 50. but what this is actually going to do is generate random values from 0 to 49 it's up to 50 but not including 50. and then so we just put a plus 1 on there and everything's good and then i can go and generate this random value so i'll just go random like that and rand num like that and that is going to give us a random value and if we run this it's going to give us there it's 30 and here it's 34 and it's just going to continue onwards and onwards now there are numerous different math functions these are probably the math functions you're most commonly going to use i'm not going to go through all the specifics this is in our math library not using time anymore so let's just replace that with math there we are and we're not using rand anymore either so let's get rid of that and you can see here are all of our different math operators and some other things that might be kind of useful well we can just run this and you can see there's the math operators and how they operate and another thing that might be kind of useful for you is can let's say you would want to convert 90 degrees into radians let's go like this that's how you can do that another thing that you can do is let's say you want to convert radians into degrees of course we can do that also and uh let's just go and print that out so something like this and like that boom so there we are and run it and you're gonna see here that 1.57 radians is equal to 90 degrees and then i'll get into if you want these to show up as like two decimal places or something just 0.2 inside of there like that i'll get more into the specifics of formatted printing actually i might do it next because i keep forcing myself see now it's just two decimal places instead of a whole ton of decimal places and that's what that does there for us and of course there's going to be all the different trig functions you could ever want so let's say you wanted to do the sine of 90. let's go like this this would be 90 degrees but what i want is the radian version 490 because the trig functions all work with radians not degrees so i'm going to say math dot sine and then pass r90 and that is the radian version of 90 degrees and if we do that you can run this and it's going to come back as one as expected and like i said there are a whole ton of different trig functions there are just about all of them and also hypotenuse you're going to be able to get and just remember whenever you're using functions that come from different package libraries they always end with a capital letter all right very important and whenever you start creating them they also will have capital letters and that is a lot of information on math functions and now i would like to talk about formatted print now go has its own version of c's printf and here are all the different codes we're going to use depending upon the different data types and you've seen a good bit of them so far this one right here is probably the one of the weirdest ones and it's just going to guess based on data type we've seen that i think in a couple examples but let's just come in here and let's go and use it so we're going to say format and we're going to say printf and i'll just throw a whole bunch of these different symbols in here and then explain exactly what they do this is going to be used for characters this is going to be used for float so we have strings integers characters floats then we'll have t inside of here which is going to have type of the supplied value like i said and i'll just go and throw some more of these in here so you can see a whole bunch of examples and remember to put a new line at the end here and you can also just skip to the next line if you'd like that is perfectly fine with go and then i'm just going to throw a whole bunch of things in here so i'll throw in stuff as a string 1 a 3 4 true and one and one oops and then go and put this quote here and go and get rid of that okay so there we go and if we run it you're gonna see all of those different data types all get placed inside of there all right and uh let's do some file format or some formatting because this is probably one of the more complex things this isn't complex though by any stretch okay so let's say that we wanted our output to be have a total width of nine what we could do and that's just going to put spaces anywhere where we do not have anything we're going to do this with our floats in this circumstance and of course this has to be in quotes so there's the quotes and then it's just though 3.14 inside of there and i'm just going to copy this so i don't have to do it again and i'm going to show you a couple examples i also showed you how to limit it to just two decimal places and let's go and just add a couple more here just so we can see something and uh let's go one five nine two and also we can go and say that we want to get rid of the decimal place like that and we can run this and you can see how the formatting was applied nine spaces you can see there's spaces that are placed inside of there and here you can also see that all the additional spaces that are going to be required by us putting the 9 inside of there are going to go before the variable and it just helps with setting some things up and lastly i already showed you this but i'm going to show it again just to keep everything all condensed for the people that want to be able to jump around by just clicking on the table of contents in the description so as printf what it's going to do is it's going to format a string and return it instead of putting it in output so let's just say something like 9 dot f and a new line and then follow that up with pi like that and then we can go and call for it to actually output onto the screen sp1 like that and run it and you can see we get the same output all right so there's a rundown of some formatted printing like i said as the tutorial continues you'll see more of it and now i want to go and cover for loops now with four loops basically what we're going to have as a standard setup you're going to have your initial you're going to have the word for first you're going to initialize a variable that's going to be used for cycling maybe you're going to have your condition that's going to determine how long we loop we're going to have our post statement which is normally going to be some type of incrementer and then we're going to have inside of curly brackets the body of the for loop and let me go let's say we wanted to create a for loop that is set up to just print out the numbers one through five we can just go like this and we're gonna say we're going to continue cycling as long as x is less than or equal to five and then we're going to increment like that each time we go through our cycle and that's first for loop example let's go and do let's say you would want to do the opposite oh we can just run this okay so why did it not print oh i didn't save it okay save it and run it again and you can see it prints one two three four five now also let's say we wanted to do the opposite well we could just put a five inside of there change this to greater than or equal to 1 in this circumstance and then in this circumstance we want to decrement and let's run that and you're going to see it prints in the opposite order one thing that's important to understand is this x value right here it is actually out of scope in main so it only exists inside of the for loop and most of the time you want that so if you would do something like say you wanted to get x like this and you wanted to print it out that's going to give you an error and that is because it doesn't exist it only exists inside of our for loop let's get rid of that 4 can also be used to create while loops so let's do something like fx like this and zero and four f x less than five and then we can go pl and fx and then increment the value for fx and we can go and get rid of this guy so we're not confused by what's going on and run it and you can see that it went and printed all those values out as well and why don't we do like a real thing here right now so what i want to do is i want to create using a wow true loop which is actually an infinite loop and i want to go and create like a guessing game so what can we do here well we can use some of the things that we've learned about before so we're going to go and get a seed that we're going to use for a random number generator so we're going to say now and then follow that up with unix and that's going to give us all values since january the first 1970. remember we have to come up here import that library all right so we have our seed value and that just adds to the randomness of what we're doing i can then i need to use it so i'm going to say rand dot seed and then throw in seed seconds like this then after that we can come in and generate a random value and we're going to try to guess what this random value is so we're going to say rand dot and we want it to be an integer and we want it to be 0 to 50. so plus 1 like that and then what we can do is go for true this is an infinite loop it's going to continue to execute as long as basically until we call a statement called break which is gonna throw us out of our loop and then to go and i'm going to call print on this and that's just not going to have a new line at the end of it i'm going to say guess a number between 0 and 50. then just so that we know it so that we can save some time i'm actually going to print it out on the screen so number is you this is a really bad guessing game if if you were uh going to print the answer right there but we're not gonna we're just gonna do that just to save some time so we don't have to guess every single number and we have these two errors we have to put a little colon in front of here like that so because these are being newly declared then after i do that i'm going to go and actually get our random value i'm going to call this guess and then follow that up with error to handle that and i'm going to go reader dot read string and it's going to read everything up until it gets a new line statement but of course i'm going to need to declare this first it's a buffered reader and i'm also going to need to go get this library that's right here as well as os so throw that in there and then also os like that and then we'll just continue working here i'm going to handle any errors that we might get so i'm going to say if error is not equal to nil well nil is returned if there is no error so in this circumstance that means there is an error and we're going to log that we now need to go and get log and throw it in here also all right so we got that value now we can go and get guess and trim any white space that might be on that so trim space and you're going to want to do this very often whenever you want to you have input that has like new lines and things like that that is why we're using it in this circumstance and then just throw guess inside of here now i want to go and convert everything is going to come in as a string so i want to convert it into a integer so i'm going to call this i guess i'll just go and get rid of that i guess and again handle any potential errors string convert and it's ato i string convert also needs to be added up here so string convert it's very easy to remember which libraries are going to be used for which so pass in gas inside of there now we can say if there is an error again not equal to nil we're going to log that error so fatal like that pass the error message then i'm going to say if i guess is greater than the random number that was generated well i'm going to print lower and then i'm going to say else if i yes is less than the random number i'm going to print higher or maybe i should say pick a lower value to make that make more sense and pick a higher value all right and then i can say else if it's not higher or lower that means i guessed it so i'm going to say you guessed it and exactly like that and then at this point we can call break to jump out of our loop and i don't know if i use it later i'll try to use it later another thing you can do is if there's a condition in which you want to stop executing code in the current loop and then jump back to the beginning of the loop instead of break you would use continue what brake's going to do is it's going to jump down here and what continue would do is it would jump up here so that's the difference between the two of those and i think i got everything all right let's go and run it it says guess a number between 0 and 50 and we know that the number here in this circumstance is 19. so let's just bring this up here and our random guess let's say we do 25 whoops accidentally typed it over here let's get rid of that and come down here 25 pick a lower value well we know the value is 19 so that is indeed correct and let's just play around a little bit longer say something like 15 pick a higher value and then we pick 19 you guessed it all right our code works and there's an example of how to use a for loop in a kind of example another thing we can do let's just go and get rid of this all together it was kind of interesting and i'm going to try to sprinkle in real you know real projects as this tutorial continues and they'll get more and more advanced as we go another thing you can do is you can cycle through an array with a range so i'm going to go and create an array i know i haven't talked about arrays yet they're just boxes that contain one specific type of data and we just come in and how we declare them is like this and i'll get more into the specifics of arrays here in light as soon as i'm done with this so i'm going to say that i want to go and create an array one two and three and i can say four well in this circumstance i am not interested in the index so i'm just going to do use that uh underscore there and then i'm going to say i am interested in each individual value of our array and then i just put our array name inside of there and like that and then i can go pl and num and it's just going to cycle through the array and print out all the values and range is pretty useful and we'll be doing more and more things with it you can see there we get one two three all right so there are some basics with using for loops more is coming but now i want to talk about arrays now an array is just a collection of values like i said before with the same data type and what's most important is the value or the size of the array is not going to be allowed to change the default values for an array if you don't put something inside of them it's going to be 0 for integers and 0.0 for floats false for booleans and otherwise normally you're just going to have an empty string like that and let's go and just declare an integer array so i'm going to call this array 1 and i'm going to say that it has five spaces inside of it and they are going to hold integers there you go you just declared an array now you can also assign values there after you're allowed to change the values in an array you're just not allowed to change the size of the array and you can see that is allowing us to insert a value in the zero index you can also go in and declare as well as initialize at the same time so we're going to say that this is going to have five spaces inside of it int and you don't actually need the five in that circumstance two three four and five and there is going to be all of those different values we can then go and get values by index so i showed you how to assign them so let's say we want the zero index for array two that is how we're going to be able to get that and let's just do a bunch of these you can go and get our array length by array and length and just by calling see if you remember what it is what's the command lin and array 2 and what else let's say that we want to iterate through it so i'm going to say i can go i showed you how to iterate with a range before i can go like this and i less than length of the array and follow that up by incrementing the array that we're working with now i want to keep it up here this works better whoops move that down and so we'll go like this and then i'll just go pl array2 and i and let's say we want to iterate with range just so that you get to see this so equal to range and you just put array 2 inside of there and it does all the work for you to make sure you don't go too far i'm going to say format dot print f and what we're gonna do here is we are going to output both the index and the actual value well both of them are integers so that makes that easy and i and v another thing you can do is you can create multi-dimensional arrays let's call this array three equal to and you have to define how big they are so let's say this is two by two and it is an integer and what i like to do is spread this across a couple lines so we'll do one and two for the first and then we will do three and four for the last and you're going to have to end that with a semicolon you have to remember that otherwise the compiler will tell you about it and let's say we want to print our multi-dimensional array we can say 4i equal to zero oh man we're gonna do this while i is less than two and then we'll increment that and then after we do that inside of this loop we'll just go copy like that paste it inside of here of course this needs to have a new [Music] index variable name so let's make it j and then we can go and print an individual all those different pieces of our array by going array 3 and i and j like that and let's go and run this because we got a lot of stuff going on here and you can see that we went through we were able to find out what was it index zero we got a length of five we cycled through all those you can see here we have our index and the value the only reason why they're on one line is because i didn't do a new line like that so let's throw a new line in and run it again and there you can see that we have both the index and the value and then there's just the values that are printed out that for in the multi-dimensional array what else we'd like um another thing we could go and create a string and then get a slice of it and we're going to talk about slices here as well also very important to understand so i'm going to go a string one like this and i'm going to throw a b c d e that's good enough i'm then going to go and convert this into a roon so equal to an array of runes i mean to say a string one and then i can go and cycle i don't care about my index i'm not going to put anything there i just want my value out of this is equal to and then you can use range here again and with this rune array that we have and then let's go and use our formatted print like we're used to and i'm going to say roon array and then i will go and throw our value inside of there and like this and then v at the end like that oh let's go and do another one whoops one thing i forgot let's go and put the array part in there all right good and another one let's say we want to convert a byte array into a string i don't think i covered that when i was doing casting so i'm going to cover it right now equal to and let's just create this and byte and this is how you create a byte array also so you're going to see as this in in having to try to cover absolutely everything there is to know about go it's it's sort of spread a little bit as to kind of use things in um an interesting way and try to keep everything compartmentalized okay so here is our byte string and this is byte array and i'll get into uh more specifics on slices and such just wanted to show you this briefly and i'm going to go pl like that and then i'll say i'm a string and get rid of the extra semicolon there like this and b str and i'm going to cover slices like immediately after this and you can see that we were able to get the code for all of these individual characters all right so i think we need to go and talk about slices now basically slices are like arrays but they can grow and they're going to have to declare them you're going to declare them kind of like var like this and then that's going to be followed up with a name and a data type but you're normally going to be using make to make a slice so i'm just going to call this sl1 is equal to and we're going to call make and we're going to start off with a string and we're going to say that it has a size of 6. now we can assign values by index so let's do something like sl1 and 0 equal to and just throw some random words inside of here okay so we got that we can go and get our the overall size of our slice so we'll say slice size and what do you think we use to get the size len remember that it's very useful okay we can go and cycle through these values with four so i'm going to say 4i equal to zero i less than length of the slice and then we want to increment it like that and we can go and just print these out using our index right like that we can also cycle using our range so i'll say 4 underscore x equal to and range sl1 and we can go pl and x for that and what else would we like to do well maybe we'll just run this so you can just see this so we go like that and run it and there we go all right uh what else uh well a slice is basically going to point at an array and what's interesting is you're then going to be able to create a slice of an array so basically a slice is a view of an underlying array and you can have multiple slices on top of that that all point to the exact same array so let's go and create this guy so this is just an array and it is going to have a size of five it's an integer and i'm going to throw some values inside of here okay so there is an array now what i want to do here with our slice is i want to start at the zero index and then i want to get up to but not including the second index i'm going to say string array and you start at zero and up to but not including the second index you can also sort of a shorthand thing you can do here is you can say that you want to get for example the first three values well you do not let's put quotes around this of course so you do not have to put the zero inside of there what you can do instead is just go string array like that and put a colon and then three all right and then we can also get a slice from from a specific index to the end in a similar way so let's say we want to get the last three we can go s array and then put two inside of here and that and just want you to know the only reason it's the last three is this is zero index one index two index so we're starting here and going the whole way to the end of course if there was you know 15 values inside here that wouldn't work um it would get way more than just the last three but whatever i just want to make sure that's not confusing now if you change the array the slice is also going to change so let's say we come in here and we say actually don't want to do that yeah let's do this so i'm going to say slice array 0 is going to be equal to 10 well if we then go pl and then we'll go and get our slice array inside here let's go and get some quotes around that and then like this well if we go and call for this to execute let's do that let's come over here and let's run this and you're going to see all those values you can also see the slice changed so it's pointing at an under underlying array so if you do change it it's going to change the array it'll also change the slice but what's even more interesting is changing the slice is also going to change the array so you can say sl3 like this is equal to 1 to change it back and then let's just go and copy this and let's paste this inside of here and then we'll say s array like that and let's go and see the changes our slice made to our array and you're going to see that it changed it back so say it went back to the way that it was um what else uh let's say you would like to append a value this is actually going to overwrite the array values if you append a value to a slice so you can just say sl3 and call append and we'll say sl3 and 12 and we come in and then say pl and sl3 go sl3 and let's just go and copy and paste this inside of here and run that and we'll call this s array and get the output for our array as well and we're on that boom you can see the results there how we went and appended a 12 onto there and how it affected both of those and also something that's important to understand is printing empty slices will return nils which are just going to show as empty slices so we can say sl4 and make and create this guy put the string outside of here so i'm going to say string and then i'm going to say that i want six spaces and i can say pl sl4 and follow that up with sl4 and also let's go and output by an index so we'll say sl4 index like that and this will be sl4 and index and we can run that and you can see the results that we get in those circumstances see it's empty and here it shows nothing but it's actually going to have a value of nil all right so there we go we're getting into the meat of the go language and now i think is a great time to start exploring functions now the basic format of a function is going to be funk whatever your function name is the parameters then return type if there is one if there is no return type nothing and then you're gonna have the body of your function inside of curly brackets so let's we're gonna start off simple and get more and more complex so i'm going to create remember if you want this to be exported from your package this would be uppercase but we don't so it's just going to be lowercase whenever i get into packages and modules and all that stuff later on and start using multiple files with go you're going to see what that's like all right so there we went and defined our function and then to call it we just say say hello like that and run it and you can see we get hello back all right let's get more complicated let's say we want to return a sum of values so let's just go and change this to get sum remember camelcase is used in go i'm going to go x and integer and y and integer so i'm defining the type and then i have to define what is going to come after what's going to be returned an integer is going to be returned and then we can just say that we want to go return and x plus y and then we can go and output this so we can just do pl or something like that and we can go and call get sum directly and then pass in five and four works and it's going to output just the result of that and let me go and clear this so that we have more room over here for output and let's say that we would like to return multiple values how do we do that well we'll call this git 2 instead it's going to get one integer and then if we need to return multiple different types we're going to put the two types in parentheses and like that with a comma separating them and then we could say something like return x plus one and we could go x plus two like that and then we can go and call get 2 and pass in a 5 in this circumstance and run that and there you go we got both of those values back and we'd also be able to assign those values to separate variables if we would like to do that uh let's go let's try creating a function that is actually going to potentially trigger an error i want to do a division so i'm going to call this get quotient like that and it is going to receive a float and it is going to receive another float numerator and denominator for this division and then inside of here it's going to return two values it's going to return an answer which is going to be a float and it is also going to return an error so we have to handle all those so what i'm protecting against is a division by zero so i'm going to say if y is equal to zero well i want to define an error message that's going to be returned with a dummy value for the answer so i'm going to say return 0 just a dummy value and then i'm going to say format and i want to trigger an error so i'm going to say here's our error and the error message is going to be you can't divide by zero it's a way of handling our errors and there's plenty of other ways to handle errors as you will see and otherwise if they didn't send a 0 for the denominator i'm just going to return the value and then can you guess what this is going to be nil all right so that is that that is the way that will work so we'll say pl and then we're going to call get quotient and then we'll pass in five and zero like that see there's no error the compiler didn't catch it so we have to catch it but if we do this you can't divide by zero is what comes back but if we then change this to four and run it again you're going to see that it does work and it also returns nil and we also could catch that nil value whenever you know if not equal to nil and all that stuff like we've done in many examples another thing that's interesting is we can work with vertical functions big jargon term what is a vertical function it is just a function that is able to receive an unknown number of values so what i want to do is i want to create a updated version of git sum i'm going to call it git sum 2 and i'm going to say that this is going to be an array of values that's how it's traded and then i'm going to put 1 2 3 like that and int and it's going to be able to receive an unknown number of values and it's going to return an int so i'm going to say sum is equal to zero and then i am going to use range to cycle through these so i'll get each individual num out of here and call range on nums like that and i'll say sum plus equal to num and then after this return our sum exactly like that okay then down inside of here what i can do is go and print this and call git sum2 and then pass in as many values as i would like doesn't matter how many and it will work and you can see 15 comes back as our answer all right so all kinds of cool things we can do with functions um let's go and cut talk about how we can get an array passed into our function and in this circumstance we are going to use pass by value so i'm going to say function get array sum and it is going to get an array and it is going to be an array of integers and then it's going to return an integer value and so again i'm going to say sum is equal to zero i guess i shouldn't have deleted that last function but whatever underscore like this value equal to range for our array that was passed in and then i'm going to sum all of these together and then i am going to return a sum like that then down inside of here i'm going to go and create an array so i'm going to call this v array is equal to and this is going to be an array of integers and let's go one two three and four works good and then i can just do uh let's get rid of this guy all together and i could do something like array sum and get array sum and pass in our array and that will work really good let's run it and you're going to see that we get our values back there and this is pass by value so any changes or anything we would do with the array in the function is not going to change the array outside of the function another thing it's interesting to know let's get rid of this is that go is going to pass the value to functions so it isn't going to be changed even if the same variable name is used in the function so we can do something like f3 equal to and we can get rid of this up here get rid of that so we can say f3 is equal to 5 and then we can print out f3 b4 function like that and show f3 and then we're going to create a function it's going to be called change value so change value and f3 and then we can say f3 after function so after function and we're going to see that nothing changed but this is very important to understand that and then after we do this we'll get into pointers okay so up here we say function change value and it says f3 so it must be the f3 right no that has no bearing this does not exist outside of the function and it has nothing to do with that value right there i will show you how to change them okay so we'll say f3 plus equal to one and we could return f3 even we can do anything we want when we run this you're gonna see f3 before function it's five f3 after function still five so you did not change it even though the name f3 is right here now if you do want to go in and change these values what we're going to do is we're going to create a new we're going to use something called pointers so i'm going to call this change value to and i'm going to change this just so we know we're dealing with pointers i'm going to call this my pointer and this is going to allow us to go in and actually access the place in memory and i'll just go score like this to access that value and change it to 12 or something like that and if we go and save that then what we're going to be able to do is let's use f4 in this circumstance so we'll change this to 10 like that and then we will go and do basically exactly what we did before so we'll change value 2 and change this to that's fine and let's go and run that and the only thing we're going to need to do to make sure we pass as a reference which means you'll actually be accessing the specific location in memory is we use the address of operator which is what it's called and then you can see that we'll actually be able to change the value inside the function oh and this is saying i'm not returning anything so i can get rid of that that's what that error means and this is my pointer not my string all right run it and here you'll see that yes indeed we change the value from 10 to 12. so that's one way we can use pointers to make changes like that we're also going to be able to store pointers so let's go and create a new variable let's call this f4 and let's go well let's call it f4 just so that we know what's going on we'll call it f4 pointer like that and let's go and actually declare this we'll go variable and we need to use our star symbol here which is in this circumstance going to signify that we are pointing to a integer and then you go and get the pointer by using that reference operator and f4 like that except this is equals because this is the first time we're declaring this and let's go borrow this guy right here and throw it in right there and name this f4 okay now what we can do is we can say pl and f4 address like that and we can go and get that address with f4 pointer right like that and let's go and also say we want to go and get the value at that pointer how you would do that let's call this f4 value value and how you would get it by using the pointer to the location in memory is with our star symbol again and there we go we can also go and assign values with pointers so we can go ptr is equal to 11 and let's go and copy this guy right here and paste it down inside of here and this will be value once again and we'll be able to get that and another thing we're able to do is well let's just run this first um well we can go and show that these values are changed so we'll just say f4 like that f4 like that f4 like that just to demonstrate this passing of values and then i'll show you how to pass a raise and you can see how the values have changed all right so rundown of pointers in using just simple um just values and now what i want to do is talk about passing arrays using pointers so let's get rid of this guy right here let's go and create an array and equal two and let's say that it is four in length and integer and one two three and four and what i'd like to do is actually pass these values to our array and then i want to double or multiply every one of these values by two and have it affect the outside world so how do i do this or the uh the world of the function main so array values is what i'm going to call this function and if i want to pass by reference so i can actually change the values in memory i have to use that and reference right there and i'm just going to print this out so i'm going to say p array like that and then up inside of here we can go and get rid of this guy altogether and we can go function double array values and array and we're going to use our star symbol here again we're going to say how big the array is that we are going to be referencing and i can say 4x equal to zero semicolon x less than four and then x plus plus like that and go and get each of those individual values inside of our array and multiply those times two and that will affect those values outside so and i don't need to say i can just save this like that and run it and you're going to see that all those when what's it say expected not sure what that's error is in reference to um i don't know run it again and there you can see we doubled all those values in the array outside of the function by using pointers another thing that's interesting is let's say we wanted to pass a slice into a function well what's interesting is passing a slice to a function works just like when we were using vratic functions so let's go and create a slice here equal to and i like this and float 64 and i'm going to throw inside what i want to do is i want to create a function that's going to get the average of these values so we can grab that guy and i'm going to use formatted printing here so i'm going to say printf and like i said i want to get the average and let's do percent maybe three decimal places and it's going to be a float and then a new line and then i'll go and call get average and what you do here is you go i slice like that followed with those three dots like that and now i need to come up here and go and create get average so let's just go and get rid of our function right here and then up inside of here say function get average and nums dot dot and float 64. see just like a veratic function and then it's going to return a float and var sum float 64 equal to 0.0 and variable and i'm going to have num size float64 is equal to float i want to do a cast so i'm going to say 64. i'm casting from an integer into a float i'm doing my best to keep covering things over and over again so this is a real learning experience instead of me just typing everything so i'm going to say 4 and i'm going to cycle through this value equal to let's use range on all those numbers passed inside of it and we'll say sum plus equal to all of those values and then after i get those sums i can go sum divided by number size exactly like that change this into lower case save it and run it and if we do we get our average is 13.667 all right so there you go a couple examples using functions and floats and pointers and more are going to be coming there's a whole lot we can do with go using functions but now i want to talk about file io all right so we're going to be able to create write and read from files so let's just go and create a file just call it f and then we're gonna go os dot and create and then we just give it some to name it doesn't matter what it is and then we're gonna say if again we wanna handle these errors not equal to nil then we're going to know that an error occurred and we can come in here and throw in both os as well as log oops make sure you put it in quotes like that okay now another thing we want to do is anytime we open a file of course we want to close the file so what we can do is we can say defer f dot close and what this will do is as long as we are in the main function the file will be open and then as soon as we leave the main function it will close basically what is determining when the file close is this closing curly bracket so whenever the you know we're out of scope it closes now what i want to do is i want to create a list of prime numbers or an array of prime numbers and again this is going to be integers like that and then i'm just going to go 2 3 5 7 and 11. and i want to create a string array from an int array and how we can do that i'm going to go s prime array and string and i will say 4 and i this is another example of where i i'm teaching numerous different topics and i'll do my best to label them in the table of contents but that's why it's better to watch the whole entire video if possible because that's the only way to learn everything okay so i'm going to say prime array and then i'm going to go string convert dot i t o a and i and string convert needs to be put up here so string convert and then we have our closing bracket right here and now what i want to do is i want to cycle through the strings and write them to a file so i'll say 4 like that and num equal to range s prime array and then of course anytime we potentially write there is a chance of an error so we'll say this and then we'll go f dot write string and i'm going to pass in each of the numbers followed with a new line so that's how everything is going to be divided up and then of course after this let's say that we want to catch any potential errors so i'm going to say if error not equal to nil alcohol log dot fatal error and then what we'll do is we will go and open our created file so i'll say file error equal to os dot open and we called it data dot text and if error not equal to nil then log as fatal and after that we could then go defer we don't really have to do this but i'm going to do this so i'll say defer close and it's it's actually better to just have this be at the end but whatever this is fine and now what i'm going to do is i'm going to read from the file and print out all the lines one by one let's call this scan 1 equal to and everything is going to be buffered new scanner f and we need buff i o up inside of here so buff i o and then we will go 4 scan 1 dot scan and just print these out and maybe put prime in front of them so i'll say prime and scan 1 dot text and that's all we're going to do for that for loop and then of course we have to go if error equal to scan 1 dot error and then just to mix things up let's do this all in one line so i'll say error not equal to nil and log dot fatal so i'm just trying to redo things in different ways so that we can see all this so after we doing and did out of that i don't think i have any errors let's go and run it and you can see there are the prime values if you come over here you're also going to see that data.text is right here say we can open it there it is so we were able to properly uh open a file create a file right to the file and then read from the file all at exactly the same time now what i want to do is i want to go and actually check if a file or i want to append a file all right so let's go and let's get rid of all of this stuff or maybe we'll just comment it out just to get rid of it okay so it's very important whenever we are appending that we are going to be able to have to set certain settings here depending that are going to determine exactly how we're going to work with a file now you're only going to have one of these options so this one here is going to deal with open the file for read only this is going to be right only and this is going to be read or write see it wouldn't make sense to open it for read and read read and write and read only or write only and re okay so that you're gonna define that one time and then you're going to define other different settings in regards to how we're going to go and append this information it probably makes more sense to just go and do it okay so what i'm gonna do is i'm going to first check if my file exists so let's go like this equal to os dot stat and does data text exists we know it exists but you know of course we're checking all right then we're going to say errors is and this is another thing we can do in regards to [Music] doing some error handling directly right here so i'm going to say is the error os dot er not exist and when this situation we're catching a very specific error okay so we're go up here oh no that's fine okay so that's what we got going on there then what we're going to do is we're going to say pl and sp print out a specific message it's going to be file doesn't exist so this is more me actually using error handling in a real scenario which would be working with files but you would use this in all circumstances this isn't something that's just specific to working with opening files this is how we can handle different errors in other situations so we'll say error and we want to keep os as well so i'm just not going to save that because it's going to disappear otherwise all right so file doesn't exist else well there could be some other situation that could come up but what we want to do is we want to come in here and we want to open our file since it does exist we'll say equal to os dot open file and the file since we know it exists data.txt we're going to plug that inside of there and then we're going to define our settings for how we want to work with this so we want to work with it oh like this append we want to say that we want to append data or and this is how we are able to stack all these different settings we'll say oh create or os you can you can put those together or have them separate doesn't matter os dot o and this is going to be read and write only in the circumstance and then we can throw in our permissions in regards to who can access the file thereafter what else can we do um well of course we want to go in here and check for any type of errors that may have come upon us so we'll say if error not equal to nil um that's fine then we will say log dot fatal and pass our error inside of there and we're getting this error up inside of here we just put a colon inside of there boom it goes away and put a colon here you need to put that colon equals any time you are assigning to something that doesn't that has not previously existed so those errors should go away now and now it's just saying that it's declared but not used perfectly fine all right so we got that there we're going to catch that error right there what else do we want to do well we want to defer closing so f dot close because remember that is no longer inside of here and then let's go in and let's write the next prime to the the file so and this is going to append to the end so we'll do error and we'll do this on same line again just to you know give you get you more acquainted with working and handling errors in the multiple different ways we can deal with them so we'll say 13 and new line like that and then we'll say error not equal to nil like that and log dot fatal error all right and if we didn't make any errors we should be able to go in there and append that information and if we come over here and open up data.txt yes indeed we see 13 there all right so that is how to create and work with files in numerous different ways we're also going to be able to pass values to our program from the command line but what i'm going to do here is i'm actually going to create a new file so let's go like this and let's give it a name like cl test dot go create that guy and what we're going to do here is i'm going to go package main more on packages later where we use different package names it's just impossible to do everything at once so what i'm going to do here and by command line i mean here in our terminal we're going to be able to pass values inside of it and it's going to do different things with those values so what i want to do is i'm going to need format and i'm going to need os and i'm also going to need string conversion i believe so here's those examples and again it's going to have main inside of it so here is main this is where everything executes and what i'm going to do is i'm going to go format dot print line and os dot args and let's do a couple different things here i'm going to get all the values after the first index because what's going to happen is if we do this well i can i can just build it so i just need to go inside of here and then i need to go go build cl test dot go like that and there it's been built and let's move this over here just so you can see it a little bit better and then i will just simply go dot backslash now if you are on a mac or linux machine you're not going to do that you're just going to type in the name of it but we're just going to do this so let's say cls clear this up okay so now i'm going to go dot backslash cl test and remember if you're on windows or mac you're going to just go and just type cl test and then i'll just pass some values inside here so 24 43 12 9 10 whatever and run it and you can see what it does is it returns an array and the very first item inside of the array is actually the app itself the executable and then you have all the values here so if you want all those values all you need to do is get rid of the app part and then continue on from that point and so what i will do is do that so let's say we want to get all the values after the first index let's just call this args like this and os dot args and i don't know if you remember that's our way of getting a slice from the beginning until the very or start from the first index to the very end we don't want the first index we just want that now what i want to do is create an array from this string array so i'll say var i args is equal to let's move this over so we have more room for our important code so that we can read it so i'll go like this and then i'm going to go and define this as an integer and then whoops don't forget to put your curly brackets in here at the end and tab that in then i'm going to say 4 underscore we don't care about that so we'll say i colon equal to and range args and then we're going to say we want a value and we also are going to deal with the potential error so we'll say string convert dot a2 ato i and that's ascii to integer and we'll say if error not equal to nil like that and another thing we can do is we can panic we can call panic and that's another way to trigger an error uh string conversion needs to be inside of here so string conversion there it goes all right so there's another way to trigger an error i args is equal to and we'll say append i args value um that's pretty good and what else can we do here let's say that we want to go and get the maximum value from the values passed inside of here sounds like something somewhat useful to do so i'm going to go max is equal to oops max there's max is equal to and i'll make it 0 by default and then i'm going to cycle through all the values that were passed into our array or passed into our command line and i'll put the maximum that was passed so i'll say range i args like that and if val is greater than max max starts at zero then max becomes the new value and then after it does that for all of these guys i'm finally gonna go format dot print line and max value and if you're wondering why all of a sudden i'm typing print line well the reason is i didn't do the alias for print line that's the reason why and we can come in here and run that all right no we can't run it it's command line oh okay so just come over here i'm gonna press up where it is there it is go build like that and then where we passed in those values and you can see the maximum value passed in was 43. okay so some stuff you can do with command lines and of course you can use a lot of what we've covered previously in the video to play around more with the command lines i think that is a good overview of what we want to do and now what i want to do is get into packages and get more into traditional go programming now packages we're also going to talk about modules packages are going to allow you to keep related code together and why this is important is go looks for package codes in specific directories now if one thing to be aware of is if you're using visual studio code like i am and you're gonna have multiple modules you're gonna get an error so what i want you to do is to go to file and preferences and settings and then inside of here you want to search and search for g-o-p-l-s and there it is we can come in here and click on edit in settings.json and whenever you do this what you're going to want to do is make sure that geopls this is not going to have anything inside of it it's just going to be two curly brackets with nothing you just want to copy and paste experimental workspace module and mark that as true if you don't you're going to get a weird error that talks about multiple different modules and things like that okay so just go and copy and paste this into the settings.json file exactly how i told you to get it and that error is going to go away i'm just going to say don't save because i don't even know what it was doing all right i don't think i changed anything okay so make sure you do that now what we want to do is we're going to be working with different files and different directories and so forth so what i want to do is i'm going to be doing a lot of different things over here inside of my terminal and i'm going to do some stuff over here okay so what i want to do inside of here is i want to make a new directory and just to keep it simple i'm going to call it app okay so here is our new app directory and then what i want to do is i need to generate a um or i need to choose a module path so and to do that i need to create a go dot mod file or module file so i'm going to come over here and i'm going to say change directory to app and now i'm in the app directory and i'm just going to type in go mod init and i'm going to type example project you can call this whatever you'd like so example project and let's move this out here so you can see this all on one line so example project and then just hit enter like that creating a new go mod module called example project okay so we can move that over there and then what do we need to do is well basically go modules are going to allow you to manage libraries and they contain one project or library and a collection of go packages and the go.mod file let's open this up here's go.mod there it is is going to contain the name of the module and versions of other modules your module depends on if they do and as well it's going to have the version of go used whenever everything worked perfectly fine here now what i need to do is i'm going to go into the app and i'm going to click on this and create a new file and i need to make a new main dot go file so there that is just make sure it's at the same level as your go.mod file and you're going to be able to have many packages and sub packages and what i want to do is i want to create another new directory inside of app so i'm going to go app like this and i'm going to call this my package and enter and then i need to make another file inside of my package now so i'm going to click on this and i'm going to call this my package dot go and make sure your package names are all lowercase and then again anything functions wise that you have inside of these packages that are uppercase are going to be accessible outside of there so what i'm going to do inside of my package.go is i am going to create a function that is going to convert int arrays into string arrays so i'm going to go package like this and let's just call this stuff and import and i need to import different libraries i'm going to use i'm going to use errors and i am going to use string conversion and i am going to use time i think i don't know let's see all right so let's go like that and then i am going to also create var name string see how that's uppercase that means it's going to be available outside of the package so i'll say derek so we'll also use that i'll show you how to run functions and then also access variables so i'm going to create a new function and uppercase now it's accessible outside of the package to string array it's going to convert an integer array into a string array that's something that we've been having to do multiple times and anytime you do something multiple times it makes a lot of sense to go and create a package that handles it for you with your so it can be reused instead of retyping code over and over again so i want a string array that it's going to return and pass that there so i'm going to say var string array and define this as a string array and then we will just simply say for underscore don't want to character i don't care about that and then we'll say like this range and then we'll cycle through our int array and go string array is equal to append and string array and string convert dot and this is going to be integer to ascii or string in the circumstance and then after we cycle through all those our function is going to return string array exactly like that all right now that we have that set up we can go over into main and actually use it so we'll say package main and then we need to import some stuff um like this and i'm not sure what we're gonna get so i'm gonna say function main anytime the package is main that means it has to have a main function inside of it and i'm going to say format dot print line and i'll say hello and i want to get the um the uh file out of here so i'm going to say i want to get this my name out of here so one thing i'm definitely going to have to format is this and of course i need format but i'm also going to need a reference to that package and what did i call it like i called it stuff so i'm going to come in here and i'm going to go stuff like that whoops get rid of this so i'm going to say like that but what i'm actually going to be referencing is example which is our module and project and my package my package like that and whoops don't do that all right make sure there we are okay so we got stuff my package now i can do is say hello like this and then say stuff and reference that name right like that and if i go inside of here and run it you're going to see it says hello derek and it got that out of my package that i have right here say there's my name so now what i can do is then go in and do the string conversion and can use those functions as well so i'm going to say equal to and this is a fantastic way to organize all of your code and normally when you write a go program you this is what you do well let's do let's stick with what we did before and work with our prime numbers all right so then i'm going to have a string array equal to and to call that function you just go stuff like that and then you're going to say array to string array and pass in our int array to verify that i actually did this the right way and i have multiple parentheses don't need those so let's go and get rid of that and then i'm going to go and print this out so i'll print line and i'll pro string array like that and then just to prove that it is what it is i'm going to go like this copy this guy right here bounce down here and then i'll go i'll call reflect on it so reflect and type of and string array and we're going to get into testing and things like that later also so here and reflect like that and let's go and test it to see if it worked and indeed it converted it into a string array all right so there you go that is some meat on working with packages as well as modules and a whole bunch of other stuff and i'm going to be doing a lot more with packages and modules and all those things later on but i am now going to talk about maps now basically a map and i'm in hello go again maps are basically collections of key value pairs and keys can be any data type that can be compared using equals to so if you can use equals two to use it then you can use that thing as a key and another thing that's important is the key can be a completely different data type than the value and the basic layout of declaring a map is you're going to type var whatever your map name is followed with the keyword map and then you're going to type inside of curly brackets whatever your key type is and then outside of it whatever your value type is all right so that's the basic let me just give you an example so let's declare a map variable so i'm going to call this heroes and map like that and i'm going to say that the key is going to be a string it can be any data type like i said as long as equals works and then we'll go and actually create a map there's multiple ways of doing this this is actually not the way you normally do it next i'll show you the other way to do it like i said i want this tutorial to be literally everything about go you could more than likely see okay so that's one way to do it you can also do it in one fell sw sweep so we're going to just go villains maybe villains and equal to make and map str the string is going to be the key and string is also going to be the value now we can come in here now and we can add keys and values let's move this up here just to keep our screen real estate nice and tidy all right so we can come in and go heroes like that and assign our key which is going to be in this circumstance batman and we can say that we want to sign the value is going to be bruce wayne like that and we'll just go and do a couple more so copy this and paste that paste that okay so the next one's going to be superman superman and this is going to be clark kent and lower case k there we are and then we're going to have the flash and the flash is having some issues in the real world right now okay so throw that inside of there um barry allen and that's the comic book flash okay so let's go and also do this similar stuff for villains so the villains are not quite as good at naming themselves lex luthor's secret identity is going to be lex luthor all right he needs to get better at naming himself okay so there's lex luthor and you can also define with a map literal so let's say super pets didn't see the movie and we can say map and i'll use a different key i'll use an int here for this and string and then we can have our key b1 and then crypto i think is his name there's crypto and then batman's dog i don't even know if this is real i guess it is if it's in a movie bat hound all right i think that's right and uh what else we can also go and get a value with a key so we're going to say format let's use uh printf here in this circumstance so printf like that and we'll say batman is and i'm gonna have it determine what the data type is in this circumstance and then heroes that's what v does and batman whoops that man like that and what else let's move this out of here because i'm not doing anything else with it uh let's say that well another thing you can do is if you access a key that doesn't exist you're going to get a nil character passed back to you and also this got out of the way so let's move that over here so let's say we went and said something like chip we want to know about chip super pet and super pets and three see it doesn't exist and you're gonna see you get a nil character returned um you can also check if there is a value or nil so you could say something like okay equal to and super pets and let's use three and we'll say pl is there a third pet i don't know let's throw that there oops like this and get this quote and put it where it is so equal like that and then we'll throw okay in there it's either going to it's going to be a boolean give you something information another thing you can do is you can cycle through a map so let's say i want to say 4k i want the key and the value and if you don't want the key you could just put undefined like that but within this circumstance we want it and the same thing with value if you only want the keys but not the values well that's how you do that and equal to let's use range again because i don't really like using four unless i have to and let's go and use print f again so print f and i will say i want to use a string in this circumstance is and i'm going to out all of my super heroes here so like that and by out i mean i'm going to list their name and um there's all that and we can run this and see what happens and you can see right here is there a third pet came back as false chip this is actually a nil character and you can see i listed all my superheroes and i guess the last thing that you might want to do is maybe warner brothers doesn't want the flash to exist anymore and so you just delete it by just coming in and referencing the flash's key and then boom it's gone okay so there you go there's a rundown of maps probably do a little bit more with maps as the video continues but now i want to talk about generics this is kind of interesting whenever i was doing some research because i want to make sure that absolutely every single thing i do is up to date when i make tutorials i go and do a lot of research i went to a prominent education site and they said go is the perfect language except it doesn't have generics what actually go does have generics and i'm going to show you that right now but basically with generics we can specify the data type to be used at a later time and this is extremely useful i'm going to give you a brief example here it's mainly used when we want to create functions that can work with multiple different data types so what i'm going to do is i'm going to go and actually create that so let's just come up here in our function area where i'm going to make this and what i need to do here well some things you need to know is the generic type parameter is going to be capitalized and you're also going to put it between square brackets one other thing that i want to do here though is i want to define what our generic can potentially be and i'm going to do this by defining a type and i'm going to say that i'm going to limit my this is a constraint using an interface i'm going to talk about interfaces later just understand that what i'm doing right here is i'm defining my constraint being that my my generic is only going to be allowed to be an integer or a float and that is it all right so i function's going to work as long as the generics that i receive are either integers or floats now you can use a couple different constraints like for example if you would use any then that would literally match anything you very rarely use it you could also have a constraint be comparable that is very often commonly used and what i mean by is if i say well let's come in here and actually create a function so i'm going to go function and this is going to go and get some and i'll call it gen just to define that it's a generic okay so what you're going to do is you're going to have your generic it's going to be uppercase like i said before it's going to be in square brackets like i said before and then you're going to have your constraint so it's going to limit what that generic can possibly be you can also do comparable you can do any but what i'm using is a custom constraint i'm saying as long as it is some type of in or float i want this to work so i'm going to use my constraint so that i find very useful so let's go and paste that inside of there and there's a whole bunch of other constraints that are actually built into go i'm covering almost every single thing you can do with go i'm not going to go through every single constraint if you want that this is where you go package go dev golang and look specifically for constraints okay and that will be on github for free you can go and search for constraints to your heart's content okay so there's a whole bunch of customized ones i'm just using this one okay so then what you do is you have your variable and then you follow it with your generic which is going to be t just like imagine this is an integer and i have int here well now it's going to be an integer or a float because i defined that in my interface that i have up inside of here but that's it so make sure that this is a t also and basically what this is saying is we are going to receive either integers or floats we want to be able to work with them and we're going to go and return the same data type good so let's go return and x plus y right like that so that's going to give us that all right so now that we have that let's go and use it inside of our main function so what we're going to be able to do is work with integers or floats so we'll say 5 plus 4 is equal to and go get some gen like that 5 and 4. and so we're working with integers but i'm also going to be able to work with floats so let's just change this to 5.6 and 4.7 we'll do more with generics as we continue here but i would love to just keep everything as simple as humanly possible all right and we got that and we run it and you're gonna see it works with integers or floats all right and one thing that is important is let's say we have this right here but we put it inside of we actually passed try to pass a string guess what it gives us an error right here string does not implement my constraint and we can't do it okay so it's going to block us from doing that and i think that is very useful all right i think that's good stuff right there and now what i'd like to do is talk a little bit about structs all right so basically structs are going to allow you to store values with many different data types in a very structured way so what i want to do is i'm going to create a struct called customer so i'm going to say customer and struct and there it is and then i'm going to find what the values are inside of are going to be so i'm going to have a name which is going to be a string an address which is going to be a string and i'm going to throw balance inside of here which is going to be a float just to show you so you're going to have multiple different data types all inside of there at the same time now down inside of main what i want to do is create a customer i'm going to call it ts customer like that and i'm going to start adding some values so i'm going to say ts dot sorry about my naming of variables i'm mainly doing it so that it is very easy to have this in a great big giant long list and you can compile absolutely everything and there's no conflicts that is the major reason why i pick the variable names that i pick okay so all i'm doing is now don't worry about it okay so i'm going to go and add some values to this specific struct so i have tom smith and he lives at five main street and t s dot balance is equal to let's say tom smith owes us 234.56 all right so that's good stuff let's go and create some more functions okay so these functions are going to be specifically associated with our struct so i'm going to come in and i'm going to say function get oops customer info and see customer so customer make sure this is uppercase if your customer is uppercase and then i'm going to do some format printing let's go like this and i want to do i want to do print f in this circumstance i'm going to say how much money that our customer here owes us so i'm going to go like this oh us and 0.2 so i only get two decimal places and a new line and then to reference values inside of our struct you just do c in this circumstance and then balance um let's go and create another function here i'd like to also be able to add a new customer so i'm going to say function new customer add pass in that customer and we're working with pointers again and address is going to be a string and there we are and i'll just say c dot address is equal to address and do i want to do anything else with it not really i think that's that's good all right so we got those and let's come back here and let's go get customer info and pass in our customer and then i would like to also go new customer ad and i'm going to use a reference so that i'll be able to change my customer um let's go um using our reference operator like this and we'll say that our customer now lives at south street and we can then come in and go po and address this and just to verify that the address was changed globally or in memory or however you want to refer to it and then let's go and also create another customer i'm going to go and say this customer is named sally smith so i'm just going to do s uppercase s is going to be equal to amphigo customer just to show you that this is what's called a struct literal and you can go and define the struct and give it values all at the same time and you're just going to put them in in the same order that they are listed inside of our struct so customer and we're going to put this inside of curly brackets like that so i'll have sally smith and i'll have one two three maine and i'll say that sally smith is good she has all of her bills paid for us and let's go and reference this to say name like that and go and get sally smith's name out of here so name um and then i think that is good let's go and run this and you can see tom smith owes us 234.56 you could see that we changed tom smith's address and that is how we get sally smith's information okay structs are more complicated let's go and uh maybe let's work with something like a rectangle or something like that so let's get rid of all this there it is it's gone and let's get rid of all of this so we're going to create a brand new struct i'm just deleting it all and come up in here okay so uh let's go type and rectangle struct like that and we're going to define that there is a length and a height you can define variables this way also float 64. so it has two variables associated with it and they're both going to be floats let's go and create some functions associated with this so this is going to be our rectangle um don't want this rectangle rectangle so rec tangle and it is this is specifically assigned to this struct it doesn't just work with a struct it is a function that is part of the struct so let's go like that and it is called area and it is going to receive or output or return a float and we're going to say return r dot length times r dot height so there is that um anything else i would like to do um i think that's pretty good so let's just do that and then we'll come down inside of here and create a new rectangle struct let's just call it wrecked one and equal to rectangle and we'll throw 10 and 15 inside of there and then we can go pl and rectangle area like that and just go rect 1 dot area and it's going to automatically work for us i need why is that wrong oh i know why it's wrong these are curly brackets so we'll just go curly bracket and curly bracket and guess what problem goes away let's run that and you're going to see we're able to calculate the area let's keep on doing some other stuff one thing that's important to understand is that go doesn't support inheritance but it does support composition by and how you get this is by embedding a struct inside of another struct so let me see here how do i want to demonstrate this i think i want well let's just get rid of this first off and let's get rid of this also all right so what i'm going to do is i'm going to go and define a contact so say type contact struct and this is going to have a first name which is going to be a string and a last name name and that is going to also be a string now let's make these camel case right like that and i'll say phone and string and then on top of that i'm going to create another one like this and these are defined types which i'm actually going to cover next it's just it's hard to cover stuff without you'll see what type means here in a minute i'm also going to find a business and the business is going to have a name and also it's going to have an address but also it's going to have a contact person that we're going to be able to get in contact with if we want the business all right so that is how that's going to be set up and then let's also associate a function with the business struct so how you do that funk and b and business this is the struct that this is tied to and let's say we want to get some information on the business or something so we could say format dot print f like that and we could say contact at and this will be the business name is and then we're going to have the contacts name and that's the first name and then this will be the last name then how we can get it i just go b dot name like that and b dot uh contact you have to reference the whole thing let's put this on a separate line here so we'll say contact and then dot l or no it's gonna be first name first name and then finally b dot contact dot last name all right and that's gonna do all of that for us and then we can come in to maine and let's go and create a new contact in a new business and then call this guy on it to print it all out so let's just go contact one like this and contact like that and then we're just going to list james oh let's give him an uppercase name so we'll say james and wang is going to be at 555-1212 and let's go and do the same for business so i'll just call this business one equal to business and we'll say that he is at abc plumbing and [Music] he lives at two or the business is at 2 34 north street and our contact is going to be contact one like that and there we are and now what we can do is just simply go business one and reference the business info and if we run it it's going to print out a dedicated right oh there's an error on line 34 unexpected new one 34. oh let's throw this right here that comma something i do all the time let's run that oops and the same problem right here so let's go like that and run it and boom contacted abc plunging is james wang all right so there you go overview of structs of course we are most definitely going to do more with structs but now i'd like to talk about defined types all right so we used a defined type previously with structs and what's interesting is you can use them also to enhance the quality of other data types which we did a bit there and what i want to do here is i want to create a whole bunch of different types of measurements so i'm going to come up inside of here and i'm going to say type teaspoon float 64 and i'm going to say type table spoon if you don't know what these these are just ways of measuring that are used in the united states and i'm also going to say and the whole point of this is i'm going to convert these to milliliters all right so that's where this is coming in so these are different cooking measurement types that we can use for conversions so what are we going to do well we're going to we're going to start off doing um like this okay so we're going to we're going to proceed to get better so we're going to say we want teaspoon 2 milliliter like this is the first way that we're going to try to work with this so we're going to say a teaspoon and it's going to return a milliliter then we can just come in and say return milliliter is whatever the teaspoon is times 4.92 and then we want to also do something similar for our tablespoons so we're going to say tablespoon to milliliter so table spoon or yeah tablespoon to milliliter and this is going to be past tablespoons it's going to return a milliliter and in this situation this is going to be 14.79 these aren't exact they're close enough all right so let's go and get those and then what we want to do is inside of main i'm going to say ml1 colon equal to and i'm going to define this as a milliliter which is going to be teaspoon 3 and times 4.92 okay so i'm gonna do this on its own and uh let's go copy this well first off let's go format dot f and three teaspoons is equal to and this is going to be 0.2 f milliliters and throw a new line inside of there and then get milliliters one so i'm doing this in in multiple different ways all right so we're going to do this directly right here and then we're going to use functions and then we're going to enhance and improve our functions so 2 like this and we'll do tablespoon so that's tbs and let that be three and fourteen seven nine like that and then change this to three tablespoons so tablespoons is equal to milliliters and then change this to ml2 all right so we're going to do this and save that and run it okay so boom boom boom there we went and got what we were looking for another thing that's interesting is we can actually go and use arithmetic and comparison operators on these so let's say we do something like po and two teaspoons plus four teaspoons is equal to and teaspoons is going to be two and teaspoons is going to be four do that and we could do uh copy this paste this down here we could also go teaspoons and is it greater than like that and then over here just change this to a greater than and see what we get and then i'm going to use these guys up here to convert with functions and then this which is actually the bad way to do these type conversions i'm going to i'm going to show you the bad way and then i'm going to show you the really great way also so i'm going to say format dot and p and let's use printf and i'll say 3 teaspoons is equal to and go and get these so and if you're wondering why i'm showing you the bad way i just i just want to show you like this is the traditional i guess i shouldn't say bad way what i should say is this is the traditional way that somebody that wasn't aware of what you could do with go might use to solve this type of problem all right so we would say something like teaspoon two milliliter and pass in three and then let's go we went and made the function we might as well use it let's do the same thing for tablespoons so this is all the same and the only thing that changes is this part so tablespoon to milliliter is that what it's called uh tablespoon oh i see what i did this is going to be tablespoon like this so t b two mil and this is teaspoon to mil i don't know why i made that upper case and that look oh i know why but you know whatever okay so let's run that and you can see hey you know we we were able to do our comparisons and also we were able to go and get the right calculations but there's a better way all right so what we can also do here is we can use at what is called an associate method with these two types and this is the way you want to do it all right you don't want to do it this way you want to do it this way okay so this is the way to do it so you say function and you're going to say that we're working with teaspoon types here and i'm going to call this two whoops two milliliters like that and then we're going to return a milliliter and then you can say inside of here return milliliter and that's going to be teaspoon whatever that is times 4.92 and this is when we're getting into the beginnings of doing kind of cool stuff all right so i'm gonna then have this be tablespoons and then have this be tablespoons so there's tablespoons two mil and everything works fine and what we're going to do down here is this is going to be tbs tablespoons times 14.79 let's get rid of this say 14.79 like that and save it and then what we can do is come over here and this is really cool i'm just going to get rid of this stuff here let's get rid of that and maybe we want to also yeah let's just get rid of all of it so it's all gone all right and what i'm gonna do is say format dot print f like this and inside of here i can quite simply just say three teaspoons is equal to those point two f milliliters like that and then to do our conversion i just say a teaspoon two milliliter like that and three and then i can just come in here and uh well let's change this actually and now and now i can come down inside of main and say something like teaspoon one is equal to teaspoon and give it three teaspoons as an option and then say like this and print f like that and point f teaspoons is equal to 0.2 f milliliters backslash n and then do our conversion very easily by saying teaspoon one and teaspoon one like that two milliliters and it will also work for tablespoons all like that nice and neat and you can see there was the conversion so right way of using these types and uh maybe i would say the natural way of using these types okay so pretty cool interesting stuff and now what i'd like to do is talk about protecting data and interfaces now by protecting our data what i mean is we want to go and hide it basically and what we're going to do is we're going to use the mypackage.go that separate directory that we were using previously and what we're going to do is we're going to create setter functions and getter functions that are going to basically hide the data for anyone who tries to use our package so i'm going to go into my package and what i want to do is i want to let's keep this function here because it's a useful function and i want to create a type and a date and this is going to be a struct and this struct is going to have a day it's going to have a month and it is going to have a year associated with it alright so we have all that so what i want to do is of course i want them to be able to change the date but i want to restrict how they can do it and how i'm going to do that is with setter functions and so let's create something so i'm going to say function and i have to tie this to our date that we have here and i'm going to call this set day and day int so it's going to receive an integer and it's going to return an error and i'm going to say if the day that they try to set is less than one or that the day is greater than 31. now there's situations where i could check the month and all that stuff and verify the date i'm not going to do that okay you can do that as practice i'm just trying to keep this very simple well then i want to create a new error another thing we've never seen before and i'm going to this has to be lowercase incorrect day value is what's going to be passed back from that and we'll need to go up inside of here and type errors so errors there we are okay so we got that inside of there now if we didn't have any problem with that then we can say all right they gave us a proper day and we are going to allow them to save it and then afterwards we are going to return nil okay now we need to create one of these for day month and year so i'm going to copy this paste that down inside of here and then this is going to be specific to month so this is going to be month like that and what else do we need to change well if the month let's change this to just simply m so we'll say m it's still going to return an error value if the month is less than 1 or if the month is greater than 12 well then we know we have a problem because there's no such month all right so we will then come down here and say incorrect month value and otherwise we will allow them to assign a month like this and we will just give it whatever they passed in and return nil i have to do the same for year of course so again set year and we're doing this in my package.go remember all right so just wanted to make sure i said that all right so in year what do i put well um i'm gonna say year if it is less than 1875 i checked the oldest person on earth was born in 1875 so it's not possible and then if the year let's change this to y y all right and if the year is greater than well what do we put inside of here well i don't know if you remember but in the past i showed you how to get the current year so it's impossible to be born in a year that is greater than the current year and if you want to get the current year that's how you do it except you're going to have to come up here and type in time and there that is all right good stuff and that is then going to change the year for our date to that and this is the y part all right anything else well yeah well if they're doing the also notice these are capitalized they have to be otherwise they are inaccessible outside of our package and these are lowercase they are inaccessible also so if you want it to be accessible uppercase if you don't then make it lowercase like i did right there alright well so if we want to be able to get those values we need to create what are called getter functions so let's create them we'll go function and d and because this is part of the date that we're working with here and then day like that and what is it going to return it's going to return an integer and we can just simply come in and say return d dot day like that and then we need one of these for all of our other different date information pieces so like that and like that up next we are going to get our month so just type in month like that and then this is going to be d for the date and this is going to be month for the month and then this is going to be year like that and then we're going to have year down here so this is how we're able to access these also notice uppercase letters all right so we have all of those set up and now let's go and use our main function that we had before if you don't remember all of this is in the app so here's my package and here is the main function all right so what are we going to do inside of here let's just demonstrate what in cup solution is okay so i'm going to create a new date equal to and it's going to be stuff dot date and like that and there's where stuff comes from this is how we're able to reference it and this is a module and my package is in the module and that is where we have all of our code hidden so let's go and get a potential error here by saying date dot set month see uppercase letters and we're going to say that it is 12 and then we're going to say if we the error is not equal to not equal to nil well then we're going to trigger an error log dot and fatal like that and then we're going to do this for the setting the the day and also the year so we got that and set let's do the day next this is the most important day in history what we're working with here and uh what else do i want to do let's go down here paste that there and this needs to be changed into an equal sign and this needs to be changed into an equal sign because we're not declaring it we're just changing the volume and this is going to be set year like that and this is going to be 1974. like i said most important day in the history of man and then what we can do is come down here and say format dot print f like that and this is actually the year that the people believe that we live in a simulated reality this is the year that the the specific date that they say is when the simulated reality began if you're wondering okay and like that and like that and then we're going to go call our gitters to get all this information so let's go like this and tab this over and we'll say date dot and month is what we want and then date dot and we will get our day like that and then we will go date dot and we want our year like that and there we go and if we run it boom and uh it's printing out the string information but you can see down here 12 21 1974. all right so that is how we can use encapsulation and packages and modules to be able to properly encapsulate our data all right so now let's talk about interfaces interfaces are basically going to allow you to create contracts that say if anything inherits it that they must implement some defined methods so for example if we had animals and wanted to define that they all perform certain actions but they're in their own specific way we could use an interface to do that and uh let's just come up here and just create a interface so i'm going to say type and animal like that interface there we are we created an interface then we're going to say anyone who inherits from this interface well you have to make an angry sound your your animal must make an angry sound and also it has to make a happy sound to equal out the world all right good and let's just move this up here then i'm going to define a type so let's say cat and it has a string associated with it and it is then i'm going to associate a function just to the cat so i'm going to say funk how you do that is you just put see cat or what you know reference that inside of it and let's say that this cat does something when it attacks just to keep it simple i'm just going to say cat attacks it's prey all right so there it is so this is associated specifically with this cat type now what i want to do is i want to return the cat's name and let's just do something like see this has nothing to do with that interface i'm trying to explain how to separate things from the interface so that the cat has its own things it does and then things that it must do if it works with our interface so we'll have that like this and this is going to just return the cat's name so there's that and you can see the cat has a string associated with it but it also wants to work with our interface and what's interesting is that you don't have to actually define the interface that the cat is going to use the interface all you need to do is create the functions and everything is automatic it just knows hey this cat wants to be part of this interface just simply by the fact that you went and created the needed methods and maybe the cat for its angry sound says cat says hiss and then for it's happy sound copy and paste you could say the happy sound is going to say and you can see that error went away because you implemented one of the functions but you didn't implement the other ones that's not allowed that's the reason why that error was on the screen there for a second so maybe the cat says per whenever it's happy okay so we got all of those things set up now what we can do is come down inside of main and let's create a cat and let's call this kitty2 cat and equal to kitty dot and cat and now down inside of maine we can say var kitty and say it's an animal and kitty is equal to cat and the kitty's name is kitty and then we can come in and say kitty dot angry sound like that and we can also call methods defined in the interface for cats because of the contract and and another thing that's interesting is we're also going to be able to call methods defined in the interface for cats because of the contract so we could say var kitty two cat is equal to kitty cat whoops it's a cat type cat type like that and kitty two dot attack and execute that and then you can also get the cat's name by saying whoops cat's name like this and kitty two dot name and run that and you're gonna see that it basically does what you would expect it to do it just executes those different functions all right so there is a demonstration of how we can use interfaces i have used them a little bit previously and we will use them later on in the video also but now i'd like to talk about concurrency now concurrency is going to allow us to have multiple blocks of code share the execution time by pausing their execution normally we can also run blocks of code in parallel at the same time and concurrent tasks in go are called go routines and not threads if that is at all confusing and basically to execute multiple functions in new go routines all we need to do is add the word go in front of the function and it is going to work so let's create a couple different functions here what's this from i think this is just hanging out there all right so let's get rid of that so let's create some functions and i'm going to give you three examples using concurrency because it is important i'm just going to do sort of just run these so you can see what these different go routines do i have to keep myself from saying threads um i less than equal to 15 so it's just gonna run this code is all a go routine is is just a block of code and we're going to it's function one like this and then we will print this out all right so that's the first one and then we're going to create another function it's going to count to 15 or something or 10. let's have it count to 10. so function print 2 and we'll change this to 10 change this to 10 and just change this to function 2 so that we can tell the difference between these different functions all right so then what we can do is just come down inside of main and go go and print let's do the 15 one and then we'll go go see now it's a go routine that's it just put go in front of it and all of a sudden go routine just like that just that simple and if we run it you're going to see that they are going to share time printing out all of this information but they didn't why didn't they well actually what happens is when the main function ends then it just doesn't even call these functions it just says hey i'm done and that's it and it doesn't do it so if you want to pause the main function to allow for the other functions to execute what we can do is we can use time to do this so we can go sleep like that and let's say we want to pause for two seconds say times and time dot second like that and that's going to pause for two seconds of course they have to come up here and throw time inside of there save it and run it and now it will work and there you can see it prints out so function two goes first well how do we have this set up we have function two is right here print to ten print to 10 comes after print to 15. so what's going on with that and it goes and prints the 10 and then it goes and prints these guys like that and we can go and copy this and throw this over here and see if that changes anything basically what is going on here now you can see things are even crazier so it's uh printing out function two is going one two and then function one gets the opportunity to print out 15 values and then it goes back to function2 and prints the point here is whenever you are using these go routines you cannot trust in what order they are going to execute so what we want to do is we want to work with something that are called channels what channels allow them to do we're also going to use locks in an example that is going to kind of define what's going on but basically with go routines you can have them communicate with each other with channels and the sending go routine is going to make sure the receiving go routine is going to receive values before it it also attempts to use them which is very important so let's come in and let's go and get rid of this so let's get rid of these functions don't need them anymore and inside of here these functions are going to print in order using their channels so i'm going to say function and nums 1 channel chan int and then we define channel like this and it's going to print these one by one so i'm going to say channel 1 is what's going to print and channel and this and 2 and channel like this and we'll put 3 inside of there and then we'll create another function that is going to just print the rest of the numbers so i'll do like this and change this to nums 2 and then change this to 456. so basically whenever these functions are called then they're going to execute each time it's almost like next which is also used sometimes whenever we're working with iterators okay so down inside of main what we're going to do is we need to create a channel so we'll say channel one like this equal to and make chan int and it is only going to carry values of one type that's important to remember so let's copy this paste this inside of here and create channel two and everything else there is the same and then we go and call these to execute so we say go and noms one and assign it to channel one and we're going to do basically the same thing with our other go routine so we'll change this to two and this becomes channel two and then what we can do is we can go and call them with print one by one and they will output the information one by one so there it is and everything will be in order and copy and let's just go paste and paste and paste there's six values we want to get and we can get channel one and change this to two and change this to two and now we will call them in order and we will know we're going to get them in order of course you could use a for loop here but i'm just trying to keep it simple and you can see now we get them in a defined order and we could run this 500 times and we would always get them in a very very specific order all right a little bit on channels let's get rid of that and another thing i want to talk about let's actually go and create like a real project this time what i want to do is i want to try to simulate customers accessing a bank account and i'm going to lock out customers so that we can't overdraw the account and let's go and actually you know make this somewhat real so i'm going to go type and account and this is going to be a struct and the struct is going to have a balance i'm just going to make an integer instead of a float and then we say lock sync dot mutex and this is this is stands for mutable mutual exclusion and it just means that we are going to be able to only allow one customer to access this account at one time so now we need to go and create some functions that can be used to work with our accounts so count like that and let's say we want to be able i'm going to use uppercase letters maybe we want to export this so i'm going to say git balance like that and int like that and we're going to go and get our lock and that and that's going to lock the account down and then we are then going to unlock the account so we're going to get the balance and as soon as we're done we're going to um unlock the account so we'll say defer a dot lock dot unlock like that and that just means whenever we get to this curly bracket and we're done outputting that it is going to unlock the account so that something else can go in and access it but what this is going to do is just simply return a balance now another thing we'd like to do with our bank account is withdraw money it's a real purpose and i'm not going to do deposit that doesn't need to be done you can do that for homework all right so i'm going to go reference our account again and i'm going to say that i want to withdraw money so let's just call this withdraw and there's going to be a value which is going to be an integer and i'm accessing the account so i want to lock it down so that nobody else can access my accounts while i'm withdrawing information don't forget to do defer to unlock the account after our uh function is done so we'll say unlock like that and then i'm going to say if the value is greater than the balance well that's a problem and so they're trying to withdraw more money than they actually have in the account well i'm i could do an error here but i'm just not going to i'm going to just keep it simple okay so not enough money in account so that's going to print out on the screen if they try to take more money than they actually have um and otherwise if not if they have the money well we're then going to come in and say printf like this and we're going to say how much money we want to withdraw so d with drawn and balance and there it is and uh new line okay and then i need to go and get said values out of here oops i lost my parentheses all right there it is and i'm going to just say the value they went and tried to take out or the value they did take out and then i'm also going to put the new balance inside of here and also i'm going to say a dot balance and subtract that from that okay so that is how we withdraw value or withdraw money out of there and uh also access the balance so down inside of main now let's go and simulate having multiple different go routines come in here and access that information all right so let's create an account let's call it a cct count like that and we'll come in and give a balance uh it's a lowercase i believe balance is equal to 100 and then we can go and access our balance just to see what it is a balance like this and comma acct dot and get balance and then what i want to do is create a for loop that's going to create a whole bunch of go routines that are going to go in there and try to take out money as quickly as humanly possible so let's say i have 12 customers all trying to access the account all at one time go routine becomes a go routine just by putting go in front of it account like that and withdraw and i want to withdraw ten dollars every single time and then i am going to call sleep on this also so let's say sleep because remember we don't want the main function to end and we'll just time this for a brief period of time we'll use time and i need to come up here and put time inside of here so there's time and there's time and i think that's all we need to do and we can run it and you're gonna see it goes really really fast but basically what happens is we go and we ask how much money we have a hundred dollars and then they try to withdraw ten dollars and you will see the balance goes down 90 80 70 60 50 and then we get to the point where we don't have enough money and no more access all right so cool stuff and that is a little bit on concurrency maybe touch on it a little bit later on but now i'd like to talk about closures all right now we're going to talk about closures now basically closures are functions that don't have to be associated with an identifier but very often they are associated with a variable so let's just go and create a closure that sums values so i'm going to go in sum that is my variable that i'm going to associate and here is the closure it's just funk like that and i'm going to say x y and both of those are int whoops and like that and it's going to return an integer and we'll say return x plus y like that now what we can simply do is go five plus four is equal to and int sum five and four and got that all set up whoops i accidentally put a period there instead of a comma now it's fixed and we can run it and you're going to see that it works now what's really great about well i'm going to cover something else let's something that's important to know is that closures can change values outside of the function so for example if you have sample one something like this and then we go change var equal to and funk and then after that sample one plus or equal to one whoops what did i do here oh that works that works okay so we have that and then i call change var and then outside of the function i say something like sample one is equal to sample one now you can see example one is one here inside the function we changed it but we are outside of the function right now so previously with functions that would have no effect but with closures you can see that yes indeed you can change those values another thing that's interesting is you can actually pass a function to a function so let's come up here before main and let's create some functions so i'm going to create a function here and i'm going to call it use function and f and func and it is going to this is a function being passed inside of here that's going to receive an integer and then it's going to return an integer right like that and then we are going oops let's go like this x and y and integer so it is going to get those values basically what it's going to do is it's going to receive a function it's going to this function is going to return an integer two parameters inside that are going to be integers and then it's going to also receive a value of x and y also both of those are integers oh by the way i just showed you here you can name both of those at the same time something different okay constantly trying to fit new things in so what this guy is going to do is then go answer like this and it's going to go and output what the function that was passed inside of it is calling saying we're going to pass x and y to that function now you can come down here go function sum values some values like this and it's going to receive an x and it's going to receive a y both integers it's going to return an integer what does this look like right here okay it's the same as this right here okay that's the difference or they're the same thing all right and then it's going to go return x plus y and if we save that then we come down inside of our main function and call use function and we go something like sum values like this past five and pass eight inside of it like that we're then going to be able to run these guys and we can run it and mix name and unnamed parameters oh what's that guy gonna do missed an error here not sure why it didn't work i just saved and then it did work so sometimes just uh if visual studio gives you a weird error just go and resave it sometimes you have to turn visual studio off turn it back on oh visual studio code anyway and then it will work and you can see here that the answer came back as 13. so we passed the function inside of there then had that function call this function past x and y that was in here and then it returned back here and then you get 13. all right so a couple examples using closures and now i'd like to talk about recursion all right so with recursion um it's going to occur anytime when a function is going to call itself and one thing to be aware of is there must be a condition in that function that ends this calling repetitively of a function now just the easiest way to understand recursion is just to see it and one of the most common things to do is to go and calculate a factorial so i'm going to say factorial and num and integer and it's going to return an integer and then we're going to have our condition right here that's going to end the calling of functions so if this number ever is equal to zero it is not going to call the function again instead it's going to return the value of one then we're going to say return number times and here is where we call our factorial function again and we're going to decrement the value of num of uh one or we're going to decrement the num variable by one as you can see eventually it will get to the point where this is equal to zero and that's it that is uh how you calculate a factorial you just go into main and just go pl like this and factorial and let's get the factorial of 4 and i'll explain exactly what's going on with the factorial if this is it all confusing don't worry about it normally an example will help out a lot and if we run this you will see that the factorial comes back as 24. well how exactly does that work well let me just show you so factorials basically this is the whole thing we pass in a 4 value that is what's going on here we then go into the factorial function and the factorial function here is the 4 that was passed inside of it and the factorial function is then going to decrement so it's going to have 4 the original value passed in right here and right here and it's going to multiply that 4 times factorial minus 1. so then you get this guy factorial 3. well guess what that is factorial 3 here this is the 3 being passed in it gets decremented to 2 and then we have the 3 here we don't have the 6 and a 2 yet that we're waiting to generate those so then we get three we pass the three into here um or we pass the two in that's this guy there's the two we decrement this down to one whenever we do that we hit our condition here where this is going to give us a value of zero this is going to become a one which ultimately makes this a two this two ultimately goes up here which creates a six that six goes up here and that's how we get our final value of 24. all right so rough overview or example of recursion you can think up some other things maybe go into google and say something like um uses for recursion and find some examples and try to code that on yourself or with yourself and what i'm going to do now is talk about regular expressions i'm just going to give you a couple examples here but basically i could do a whole entire tutorial and i've done entire tutorials on recursion you can just type in regular expressions derek bannis and google and you're going to see a whole bunch of them because i've done them i'm not going to get into that but i'll give you a couple examples and i'll cover all the specific changes that you will get with go so we're going to go inside of main here and i am not going to do any functions or anything like that so we can just come up here all right so let's say let's create a string like this and what i want to do is this string is going to have the ape was at the apex like that all right so what we're going to do now is i'm going to create a regular expression and it is called a match and i'm going to show you a couple different ways of using regular expressions one's going to be compiled and the other one's not going to be compiled so we'll say match like this and i'm going to create a regular expression so i'll go regular expression dot match string and then i'll put the regular expression inside of it and a regular expression is just a series of codes that i help you identify what you are searching for now what i'm specifically searching for are the letters ape ape so i'm going to put ap e in like that now what i'm also looking for is the letters a p e and i do not want that's what this caret is it's a not i do not want that to be followed with a space so i'm specifically looking for apex in this situation that's what i'm trying to match i'm trying to not match this because there's a space there trying to match apex so anything that is not followed by a space is going to work and i am going to come in here like this and then go r e s t r like that and this is just going to tell me if i have it or not if i have a match or not just come up here regular expression like that and then come down here and go pl and match like this save it run it and it's going to say false okay so it could not find that why could it not find that well in this circumstance this comes to an end there is nothing there all right so that is the reason why we were unable to match that another thing let's go and actually do a compiled regular expression so i'm going to go r e and str2 and equal to and i'm going to go cat whoops put this in parentheses i'm going to search for cat rat matt whoops matt fat and pat okay so we got all those things in there i'm gonna do a compiled regular expression like this underscore equal to and i'll go regular expression and compile and then we'll be able to use this in a lot easier ways and compile i would think i know how to spell that right uh compile is just c-m-p-l-a-e okay so compile and inside of here the regular expression is i want to match these letters c r m f and p and then i'm looking for the letters at to follow after that so those are the specific things that i am looking to search for now what i can do here is i can say let's say i want to find out did you find any matches and i'll go pl like this did we find a matching string or not let's go like that and we can just go r like that our reference to our regular expression and match string like that and then inside of there we're going to put our string so we'll go r e s t r 2 and there that is and i'm going to copy this and i'm going to do similar things for finding other different things let's say that i want to return the first match i can say let's just call this find string and we will go and just go r and we're going to change this to find string so we'll do fine string like that and that stays the same up next let's say that i want to return the starting and the ending index for the first match let's change this to index like that find string or this is yeah this is going to be fine string like that and index like that so let's get that whoops string index like this and also that's it that's all i need to do so another one let's say i want to return every single match i have so what did i do here i typed in index g i don't want that to get rid of that g okay so i'm going to return every single match and this actually comes back as a array so you could cycle through it or whatever you'd like and let's come get rid of this and i'm going to change this to find all string like that and what you need to do here is change this to negative 1 if you want to find every single string i'm going to do something kind of similar let's go copy like this and paste this inside of here and in this situation i want to find the first two matches so i'll just say first oops let's just go first two strings whatever there's that and find all string and i'll just change this to two that's what i'm looking for and then what i can do is i can go and get indexes for every single match and this one is a wordy one so we're gonna say something like uh all let's get rid of this i'll change this to all sub match index whoops in the sub match index there we go and then we will go and cycle through all these and this is actually find all string and then it's going to be sub match index like that and then we'll pass in our oh there is our string right there and i'll leave this as negative one as well and uh let's say i want to replace all matches with the word dog for our final example so throw that there i'm going to get rid of this all together and like that and then i'm going to say replace whoops and it's on output this i'm going to say replace all string and inside of here and what i want to do is put the word dog everywhere we have a matching string and i think that is just about every single thing oh we got an error let's figure out what that is and again it was a weird error that was brought about because i uh i had to resa i had to save it i don't know uh visual studio code is messing up a little bit all right did we find a match yes fine string and the fine string in this situation is going to return the first match which is rat you might say why is it matt not matching cat this is lowercase that's uppercase index it's going to find the beginning and the ending of that match there it is we're going to get all the matches right there as an array first two strings and then all sub match indexes again it's going to give you the beginning and the ending for all of the matches index wise and here you can see we replaced every single match with the word dog okay so there's a rundown of a whole bunch of different things you can do with regular expressions again if you want to learn more about regular the really awesome thing about regular expressions is they almost always work exactly the same in every single programming language so if you learn how to use them just type derek bannis regular expressions in youtube boom you'll find a whole bunch of things but now i want to talk about automated testing now with automated testing automated tests are basically are going to make sure your program still works while you change your code and because a common error is as we're coding we will go to try to have our code do something very specific and it will do it the right way and we'll say great it works and then we'll move on and we'll do something else and whenever we do that sometimes we break the code that used to work so automated testing allows us to go and verify that even though we made a change that we didn't break anything that we created in the past so what i'm going to do i'm going to come into my go tut here and i'm going to create a new directory so bounce inside of there and i'm going to call this app2 for simplicity reasons and then i am going to over here going to change change directory to uh let's go back and then we'll just say change directory to app2 like that alright so we're in app2 uh what i want to do now is inside of app2 i'm going to go and create a file and i'm going to call this test email like this and test dot go all right so here is a brand new test email test dot go thing and then over inside of here i'm going to go go mod init app two module we've already talked about modules if you skipped that part well then go back and look in the table of contents all right so there we go and we created that module now i have to go into these guys and actually create both of them so i have test email test and i need another one and it's just going to be called test email for some reason in my wild brain i thought i already did that so i'm going to call this test email dot go these these have to have the same name so i have test email go and uh test mail underscore test okay so it's test email dot go whoop dot go i need that file right there created that's what i'm currently in um let's go like this and like that and then i'm going to have another one and let's go copy i'm just doing this in case you can't see underscore test go alright so those are the two files that i'm going to create and i'm currently in test email right now so i'll go in here and test email go and i have to create a new package so i'll go package and i'm going to call this app2 and i need my imports again and i'm going to need format and what this is going to do is it is going to verify that i have uh an email that works so i'm going to pass a function i'm going to have a function i'm going to pass it an email and it's going to say hey that's a valid email hey that isn't a valid email so i'm just going to call this is email again i want it to be uppercase so that i can go and get this from outside the package uppercase and i mean the is email part is uppercase this part right here has to be uppercase okay so and then what you need to do is go string and error like that on that and here what i'm going to do is i'm going to use a raw string here so that i don't need to double my backslashes this is a regular expression so here's another example of how to do a regular expression and i'll sort of sort of step you through exactly what's going on with this regular expression so regular expression dot compile and then i'm going to put inside here oops i want this to be a raw string so for raw strings and go you do a back or a back tick and here's the regular expression so basically what i'm saying here is the beginning of the email is going to have letters or numbers dots underscores percent signs plus signs negative signs and it's going to have 1 to 20 of those and then it is going to have an at symbol and then it is going to again in what follows the at symbol is going to be letters or numbers or whatever you dots and dashes there's going to be between 2 to 20 of those and then there's going to be a dot and then i'm going to have any type of letter uppercase or lowercase and i'm going to have two to three of those okay so it's a generic email it might not work in all circumstances but in many situations it will work and like i said this is a tutorial this isn't meant to be the world's greatest email catcher all right so what i'm going to do is i'm going to say if r and i'm going to call match string if you remember what match string does if you don't go back a couple minutes and learn about regular expressions so i'm going to say match string well this is going to be s in there in this situation because the string was passed inside of here all right and if i get this if this gives me a positive i'm going to say return and i'm going to say something like valid email like that and then i'm going to also put nil inside of there what's that mean that means that this was a valid email and we didn't get an error otherwise i'm going to say return and i'm going to return nothing and then i'm going to return that i want an error that an error occurred with this and i will say not a valid email like that okay so that is what this guy's gonna do now over in the test part of this guy again i'm gonna use the same package so i'm gonna say package app2 and i'm going to say import and this is where we're going to be testing everything call this testing like that and let's go to the next line and in here i'm going to have a function and function and it's going to be called test um whoops capitalize this test is email same exact name except there's a test in front of it as this this is called it is email this is called test is email and we'll put a t inside of here and this is what we need settings wise to be able to verify this whether the function is operating properly so i can do an underscore like this and error colon equal to call is email like that and pass in hello all right clearly not an email jump down here i'm going to say if error equal to nil well in that situation i'm going to go t dot error and i'm going to say hello is not an email like this so we can go and do that testing and then what we do is we bounce over here to run our tests make sure you're in the right directory i'm in the app 2 directory so bounce over here and to do your test you just say go test dash v like that and run it and it's going to come back and it's going to say hey it passed that means that your function is operating the way that it should work all right so that is a positive thing now we're going to do something similar here we'll do like this and what would happen is you're going to run this every single time and then you're just going to automate it it's just going to do it for you and hey by the way i'd like to check to make sure i didn't break his email somehow and you just keep running it remember if the first time you declare a variable you're going to put the colon equals and afterwards you're not okay so i'm going to say is email and then this situation i'm going to say derrick oops derek at where's my ab symbol at aol.com so there you go that is a valid email and in this situation i'm going to say is an email so i'll say dirk at aol.com is an email and let's do another check inside of here also and we'll bounce down here and we'll say [Music] and what we're going to pass in this time is derek at aol knock the dot com off of there and uh in this circumstance we would say not equal to because we're on purpose going to do like the reverse for this just so we can see that the test worked and there was an error we're sort of artificially creating that error and we'll go in here and create this like that jump over here and run our test and you can see it's going to fail see it failed and that's what it did but i mean i on purpose made it fail by changing that no equal to and not equal but whatever so i think you get the odd the idea of automated testing and it normally wouldn't work like this you're you're mainly going to use it in circumstances in which you just want to verify and you want to write additional code that isn't based off of his email but that's what you get all right okay so there you go a whole bunch of other things and for my last example it's going to be rather a little bit more complicated i'm going to develop a web application now our go apps are going to be able to be run directly inside of our browser on the back end and so to create one what we're going to do is we're just going to go to go tut again and we're going to create a new folder and i am going to call this just web app and then inside of it we're going to create a couple different files so first off i'm going to create a new file and i'm going to call it webapp.go and i'm also going to create another file and it is going to be called new.html and we'll create another file and it's going to be called to do's and we're just going to store everything on the file system rather than getting involved with databases and then on the final one what else do we want well we want our main view.html file so there we go we have four files and we're going to start off in the web app part of this little application and we're basically just going to start off everything we're it's just the same sort of stuff we've already covered and really the only difference is is we are going to be using a couple new libraries but also we are going to be using some notation inside of the html to get from our go application to the browser so what are we going to import a good bit of stuff so let's go import and we're going to be reading and writing from the file system so we're going to have our buffered io like we've used before we're going to use format again then to work with the html we're going to go html template and we are going to be logging errors and net http is going to allow us to respond and make server requests and also we're going to need os because we're working with the file system like i said before and this is just going to be extremely simple to do application it's very very simple i want to keep it simple so that it's understandable so we're going to create a struct and it's going to be called to do list and we're going to keep count of how many things we have to do and we are going to have a array inside of this which is going to be our list of to do items and let's get our error checking and let's throw it into a function and let's just call it error check receives errors and then we're just going to say the same thing we've done countless times at this point you should really remember a lot of this stuff and nil and we'll go and log any type of error we may have received so there's that what else are we going to do well basically we're going to have to create a writer and it's going to allow us to write to the browser so to do that we say function and write and writer http dot response writer and let's move this out of the way here so we have a little bit more room this is actually not going to matter much this time and basically with it we're going to create a message and add it to the response that is then going to be displayed inside of our writer so here is our message and we're going to pass the messages to this function it's going to put it in the browser then let's go and we're going to need to perform some type conversion to bytes to be able to use this string message that is passed inside here so we'll say writer dot write and this is going to be a byte array that's what we want to convert to so this is just casting like we've done and we can just go and call our error check and pass our errors inside of here to handle that all right so after we have the writer all set up well now what we need to do is create individual uh messages that we are going to write to the screen based off of the directory they go to on our website so let's say that we just want to do something really simple we're going to say hello in english and in a couple other different languages so say english handler and we're going to have our writer passed inside of here http and this is going to be a response writer so response writer and also we will have our request that's going to be sent inside here i'm not going to go over how the you know the internet works in this video there's plenty of other things i've covered here and i don't need to do that also and then what this is going to do if you want to write to the browser you just call your write function we created and pass our writer inside of that and then whatever message we want to display now like i said very very simple so this is just going to say hello internet like that and let's say that we would like to also be able to and that's it that's literally it so whenever they go to a specific part of our website a directory we are going to automatically call this and output this to the screen now we could also output html which i will show you how to do and we could do this for a couple different languages why don't we just have it be really simple and just do two so we'll have a spanish handler and then this is just going to be hola so there you are and you could do some more if you'd like all right so that is how we're going to be able to very quickly just output a simple line of text whenever they go there another thing i'd like to do is to interact with them and what this is going to do is actually put a view on the screen and then it's going to use some different templates to be able to output a slightly more complex uh web page so let's just call this interact interact and this is going to display a list of to do's and things like that so i'll call this interact handler again it is going to receive both of these so let's get this and there we are so we got that set up and then what this is going to do a little bit more complicated i want to get our i'm going to have like a text file to do so let's let's come in here and say something like buy dinner and wash clothes okay there we are so we have this list so what i want to do is i want to be able to get that text information so to do so we're going to say i'm going to call this to do vowels oops vowels like that is equal to and get strings and this is going to be to do's dot text and i'm going to create a function that is going to get those strings so let's just create that first where do i want to put it let's put it up let's put it right here so i'm going to come in here and go function and get strings and we're going to be past a file name and which of course is a string and it is going to return a array of strings and so let's go up here i'm going to go var lines we're going to cycle through and grab the lines of text one by one so that's going to be called lines and i'm going to try to open the file maybe it doesn't exist equal to and we'll use os dot open so we're just using the same stuff again we're just adding incrementally more information so we'll say file name and of course if we do this we have to go and check that the file actually exists or not so is not exist and then error throw this here and return nil and then let's call our error checker to handle that for us so error check error and of course we're going to want to close our file so we'll defer to the function closing and that's when our file will close so there that goes and then we also want to go in here and read lines of text from that to do's dot text line that we have right there so to do that we are going to get a buffered scanner so there that is and buffered io dot new scanner and pass in our file to handle that and then we're just going to cycle through all of the individual lines and scan those in and then we'll be able to display them in our browser so like this and lines is going to be equal to append lines and scanner text and boom and again after we do all of that we can do another error check if we would like pass in well this is going to be scanner error in this situation so we'll say scanner dot error there we are and then after we do all of that we want to return our lines of text which we will then use and display in our browser okay so we got all of that set up and i'm getting an error here for file name that's because that's not capitalized anything else no okay so now we come back down to our interactive uh handler that's going to you know do its display to-do list information and maybe add options for them to add additional to-do's and things like that so another thing you might want to do is if you would like to print to your terminal as the your program runs you this is how you do it you could do something like just print f like that and maybe we want to print out our list of to do's or something like that so we could do this number v and their throw a new line inside of there and this will be to do values so that's going to get that and get rid of my format yes it did so let's throw format back inside of there i'm back down inside of here so that's the way you can print to your terminal to see what's going on with the website you could print out errors or whatever you would like to print out i guess now what i'm going to need to do is i'm going to create a template that is going to use an html file to display this information on the screen so go template error like that template and we call parse files and we're going to be mixing together our template our html and plug in all of the information we want let's call our error checker again and then after we do that i want to create a to-do list with the number of to-do's as well as the to-do's that we have inside of here so i'm gonna go to do's like this and to do list and we will have our to do count and that is going to be equal to the length of our array called to do vowels all that's to do vowels like that and also we're going to have our list of to do's which are going to be in that array so to do valves and then we'll cycle through all of those to-do's and display them on the screen also i want to write the template to the response writer so let's go like this equal to template like that cute and pass writer and to do's and i'm getting an error on template up here because it was deleted so let's just come up here like this html and template it's really neat whenever it all comes together here so just bear with me just have to do a couple little changes here and there what else would i like to do inside of this well i'm going to have to go and connect the different directories to the different function executions um [Music] and i'm just thinking do i need to do anything else with that well one other thing i'd like to do is allow them to add new to do's so let's go function new handler and [Music] writer let's just grab this so writer always using the response writer and the request itself so copy and paste us inside of here like that and let's get this now create a again you're going to do the same thing you're going to create a template using the html i'm going to write the html here in a minute so i'm going to say template error equal to template and again you're going to do parse files and the name of this html file is going to be new.html and it's just going to allow them to put a new a a new to do into it call our error check again save ourselves a little bit of time and then error is equal to template dot execute and writer and nil all right so we got that set up and now what we're going to be doing is we will have the user go and enter a new to-do using our new html page and then we're going to whenever they do that we're going to call another function and it is going to store the new to do and then after it stores the new to do it's going to send them back to the indirect or interact page where they'll get a list of to-do's and then the option to add additional to this so we'll say create handler and again do our writer and response all that and we're going to go to do equal to request and this is going to be the new to do that we are going to write to our text file so to do and this needs to be inside of quotes and this is the value that's going to be passed from the form to the create handler i'll create all the html in a minute now what i need to do is i need to define the options for working with our file so i'm going to say options we've covered this also os and i'm going to say read and write is what i want the options to be so underscore read write only and we'll stack these and also have appends like that and we'll also have create os dot o underscore create and then we will have we'll actually open the file with those options so we'll say file error potential opening files we'll say os dot open file and the name of the file is going to be to do's dot text and we have to list our options that we're going to use and let's go down to the next line so we have a little bit more room and then i'll also have the permissions for this so i'll say file mode and 0.60 all right so we have that set up and after we do this let's do another error check like that let's go in up here and then i'm going to append new text to that file so this and error is equal to and call format dot f print f f print and we'll do f print line so everything's on a separate line that makes sense and to do come down here and again we'll do another error check like that then i'm going to close our file so i'll say file dot close could use defer if you wish do another error check just in case something went wrong and then after we got that that information from the form and we wrote it to the file and we closed the file now what we want to do is a redirect to another part of our website so to do that and that's going to be back to the interact page which is going to show all the to do's and also the option to add additional to do's so we'll say re direct and pass the writer pass the request and then we're going to display where we want to send them and this is going to be backed to interact and then you also are going to have to put inside of here the a status and what we're going to use is status found and that's okay and there you go that's all of the main functions that we're going to use except for the main function so just verifying yes that looks good okay so what we need to do now is go and create our main function and all it is going to do really is it's going to tie different directories in our website to all these different functions that we have right here so i'm going to say funk and main like this and http dot handle funk and then if they go to the directory called hello it's gonna do that english version of hello so we'll just do english handler like that and then we're gonna make one of these for each of our different parts of our website so let's do hola so hola and then this is going to be spanish handler so spanish handler and do another one and this is going to be our interact page which is going to be our main part where it's going to list the to-do's and it's also going to um give them the option to add additional to-do's we're going to have our new page which is where there's going to be a form that is going to allow them to add new to-do's so we'll say new handler and um this is not what i called it new handler oh new handler i just have this lower case so let's make it like that and come down here and that's good now and then the last one is going to be create and it's a temporary stop off point we go from new to create create rights to the file and then create sends us back to interact where we did our redirect here so create and then this is just called create handler so create handler and then all we need to do is add the ability to listen for browser requests and respond to them and it actually is only ever going to receive a value if there is an error so it's just going to sit there and do its job so we'll say listen and serve and we are going to define that we're doing this on a local host because that's easy and that's going to be our 8080 port and then pass in nil and like i said this only is ever going to get a value if there is an error so we want to handle that potential error like this and there we go and there we go all right so that's all the code that we have and now what we need to do is just go inside of here and write all of the html so again this is just regular html so we're going to say html and we're going to do something like to do list like that and then we're just going to create a div i'm keeping this extremely simplistic so let's just go div now what we want to do here is we want to display the number of to do's that are have come across so we just use this template so we'll go to do count and that is boom to do's to do's all right so there you are it's going to take this guy or is it over here it's going to take what we have stored inside of this struct to do count and it's going to display it inside of our web page one thing that's also kind of interesting or useful is maybe you want to put some comments in your code this is how you do that so we could say something like displays number of to do's all right so that's how you do a comment inside of go all right and that'll be inside the browser and of course it won't show another option i'd like to do is let's say that i want to give them the option to add a new to do you just go like this and this is going to redirect them to the new directory and there we go and i can do something like add a to do and if they click on that it'll go to the new directory they can add a new to-do it's you know pretty straightforward and then what i want to do is under here i want to cycle through and display all of the to do's that we have stored in our file so again um i don't need to do that how you do this is you just say range and then your to-do's which are in this struct say to do's right here we want to get all of those different arrays and we want to cycle through and print each of them on the screen one by one so we'll go to do's like this and then let's say we want them to be separated so we'll put them inside of paragraph tags and then to get the individual thing that comes out of our array we just put in these curly brackets and a dot and then closing curly brackets and that is going to do that and then after this to end the ending for our our looping structure that we have here is just to type in end okay i think i did all that right the only other thing left is our new page and we're going to have to commend it and go and provide a simplistic form so i'm just going to say h1 like this add a to do and then let's go and create our forms system here so let's just say form action and where did we say that we were going to send these commands well to the create directory or this form information anyway actually this form word um and then we're going to use post for this and let's get this out of here and there we are okay so that's good and then after we do this let's throw this in a div and we will get input from our user which is going to be text it's going to be whatever the to do is that they want it to be i keep doing that because the autumn auto completion changes whenever you're doing this okay so we'll say to do all right and that's what will store its name as now if we go back to this you'll be able to see it so where is create create is right here and the form value is to do right there and that is how we are getting it from our browser so there is our to do and then afterwards let's do this inside of another div we are going to just have a submit button so we'll say input type is equal to submit okay super super super simple nothing that complicated all right so do this and close that off and save that now if we want to go in here and actually run this in our browser as long as i have everything set up properly well first off i'm going to have to go into the correct directory which is web app so change directory to web app and if i go directory you can see all the files there if they were there um let's try directory again yeah there you go you can sort of see them anyway doesn't really matter we know they're in there so what we're gonna do now is run our web app and to do that you just go go and run and web app dot go and as long as i don't have any errors it should work looks like it did and if i go to localhost it says hello internet which you can see if i zoom in like that and the other one was i don't even remember what did i call the spanish one okay so let's go back into our web app and let's open this up don't need this at all anymore that's going to continue running until i shut it down with ctrl c uh hola that's what it was so let's go hola and like that and it's gonna go ola internet and then interact was the interact was where we were going to have our to-do list there it is there are two to-do's i should put a new line here because that looks bad anyway you can see we were able to read from the file and display those of course you could use databases and all that let's come in and add a new to do what are we going to have here um run all right let's say clean freezer okay so there's that is and clean freezer shows up all right so there you go guys there is a rundown of the golang language i did my best to create a full course on it that would cover just about anything you could ever imagine or at the very least i think that you will have no problem learning more at this point which i think if you work through and run all the code and experiment a little bit you're really going to master this language if you want to install go obviously you have to you're just going to go to go dot dev and download and then you're going to find your choosing operating system and you're going to click on one of those guys it's going to open up a auto installer of course you're just going to click next a whole bunch of times except terms next again you're going to it's more than likely just put it in the default directory that's what you want to do and then you want to click on install and then after that finish and then if you want to go and verify that go's been installed you're going to go go version in your terminal or in your command line and you should see something along the lines of 1.18 or something like that then you're going to want to set up visual studio code so you're just going to want to go to your extensions looks like little tetris pieces over here and you're going to type in go and then you're going to want to download this guy or install this guy the very first result that comes up then you're going to see a window that looks like this and down here in the right hand corner you should see a uh installation it's going to tell you to install install some different requirements you're just going to click on install and all kinds of stuff is going to go on over here and a whole bunch of things are going to install for you at this point is probably a good idea to go and just do a basic hello world which you should have no problem doing just copy this code right here then over in the terminal part you're going to want to change your directory to whatever directory you want you know you have your app your uh file stored inside of and then to run your application you're just going to type go run and whatever the name of your go file is and you should see hello go another thing you should do is update everything so you're going to want to come up here and this is going to be in your command palette so you're going to go view and command palette like that and type in go and start typing in update and this is going to pop up and you want to click on this guy and then you want to select every single one of the things that pop up down the side here so you just put check marks on all of them click on ok and then a whole bunch more stuff is going to install for you and then on top of that if you want to run everything in the terminal you're just going to go to your extensions again type in runner and you're going to see code runner pop up inside of here and you're going to click on install of course and code runner is going to install then you'll be able to run all of your code directly in the terminal on your screen but to finally get that to work you're going to want to go into your settings it's under preferences and then you're going to type in at ext formula hendry here let me show you actually where it is it's actually here let me show you where it is so you're going to go to extensions whoops like that and you're going to find your code runner so just type in runner run open that up you're going to go into settings inside of here extension settings and you're going to scroll down until you see run in terminal right here and make sure that this has a check mark on it exactly like this and then what you'll be able to do is take your main little hello world program and directly run it in the terminal just by clicking on this little play button up here and there you go that is the end of my video if you want to recommend what my next language should be i'm going to be making absolutely huge videos like this for all the programming languages out there so like always please leave your questions and comments down below otherwise till next time
Info
Channel: Derek Banas
Views: 80,026
Rating: undefined out of 5
Keywords:
Id: YzLrWHZa-Kc
Channel Id: undefined
Length: 229min 15sec (13755 seconds)
Published: Fri Aug 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.