Golang Tutorial #2 - Variables, Data Types, & Declarations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
happy new year and welcome to the third video of this go lane tutorial series in this video we'll be covering basic gold variable types and variable declaration if you're already familiar with gold primitive data types feel free to skip ahead to the declaration part using the time stamps in the video description and if you're more of a visual learner make sure you stay until the end because i'll be using a graphical demonstration to drive the point home on variable declaration let's get started in the last video i showed you guys how to install visual studio code and golan as your ide uh but for this video and probably the remainder of these tutorials i'm going to use an online editor called go playground and this editor doesn't have a ton of functionality but it's good for you know writing really quick code and sharing code snippets uh and so i think it probably works well with what i'm using it for and honestly your ability to code to be tied to any you know specific ide so like i said this works for us um what i'm going to do here is type out everything i'm explaining in comments and for those of you that are new comments are just a way to make uh notations or annotations in your code and the comp the compiler doesn't see these things you can do a single line comment or a multi-line comment that's kind of how you do that so let's move on to variables what is a variable so variable is basically a way to store a value in memory and these values this data is stored as bytes which are broken down in the bits and this is just basic level computer science stuff and a bit is the smallest unit of data or memory in a computer some of the types of data that you can store uh one is an integer and you can have both assigned or an unsigned integer there there are variations of these signed and unsigned integers based off of the size of the value that they can store for example unsigned means that it's a positive number so you have an unsigned 8-bit integer which holds the value 0 to 255 you have an unsigned 16 bit which holds 0 to 65 535 and then the rest of these i actually not going to say the numbers actually have you written in front of me because i don't know them by heart um and then un-64 just holds a really big number okay and then you have a sign integer so these can be either negative or positive so you have an n8 and you notice an eight is actually the same size as a u and eight uh but it just goes in the negative direction i'm just going to type these out because it's kind of hard to talk and type at the same time so these are all of your types of integers and just so you know these are often referred to as primitives everything i'm showing you today is like a basic data type and this is common across other programming languages and you also don't have to include the number you can actually let the computer figure out what size this kind of integer variable should store and it's based off of your operating system so we'll either hold a 32 or 64-bit value and so when you hear you know here people say you have a 64-bit or 32-bit operating operating system it's talking about the default uh storage size for storing data uh in addition to integers you have floats so you have a flow 32 and these are decimal numbers so example is like 1.24 load 64. then you also have booleans which hold i'm sorry actually uh bytes i'm getting ahead of myself you have bytes uh which are uh the same thing as u and eight so that's an eight bit unsigned integer then you have a room which is the same thing as an n32 now you have boolean which holds either a true or false value and then you have strings which basically is a readable text so anytime you're like reading a website um you know you see the text there typically that's coded as strings and for that the way you do it or the syntax is that you have to put quotes around it so those are your primitive basic types and go as well as many other languages so how do you actually declare a variable how do you you know create that storage space well one way you can do it is through what's called explicit declaration and when you explicitly declare something you're telling the program exactly what type of data you want to hold in that variable and so you have to use this keyword var followed by the variable name followed by the type and what that does is kind of create this location that holds exactly that it holds a type so let's say we wanted to create a location in memory card greeting that holds a string well as i said we do var the name of the variable and then the type so we've actually got this place in memory but we don't have a value you know it doesn't it doesn't have anything in it right now uh to do that what we need to do is actually create our main function here then let's see let's say that we want to and just so you guys know the act of active storing a variable is called initialization so let's go ahead and actually you know give our greeting variable of value and to do that all you need to do is this equal sign this is the assignment operator so we're not going to go with hello we're going to go with what it is what it is and then we can print that out greeting now let's run it let's see all right and there we go so that is how you declare and initialize a variable in two separate steps but you actually don't have to do it uh in two steps you can actually do it all in one step if you choose to and so the act of doing this is actually called implicit declaration and what you do is you declare and initialize it all on the same line using this colon equal operator so here's an example if i want to create a variable that holds an integer i just use that operator set it equal to 30 and the computer basically determines or figures out what you meant to store based off of you know what the value looks like so let's go ahead and print that out and you can see that it prints out 30. uh let's go with just a few more examples so we'll do year we'll set that equal to a string of course it's 20 21 now um we'll do another string that is happy new year it's space uh and then we'll also do a boolean so we'll set this boolean equal to five is greater than eight right that's what we're assigning it to it's going to figure out this expression here and assign the value store it in this variable called my boolean and lastly just so you guys know as i said this is called implicit declaration because we're not stating the actual type it's implied you can actually use the var keyword in conjunction with uh implicit declaration as well so the way you do that is you use bar the variable name the data type and then you just use the equal sign you see here we don't actually use the colon operators with bar and then i can run and actually let me i think i need to actually print it out first so let me first make some print statements if you guys see me switching back and forth between monitors because i've got one monitor that's recording and then the other one that's kind of like we're actually code on but that one is too big for recording so that's why my eyes is like darting back and forth but if i go ahead and run this all right there we go so we get all of those things printed out so hopefully that makes sense um like i said i've showed you how to use implicit and explicit declaration and now you know all of the basic primitive data types and go as i mentioned if you still don't understand kind of what's happening in memory because that is important to make sure you understand that here is a visual demonstration to kind of drive that point home imagine that these are locations in your computer's memory each location has an address and for now we'll just focus on one of these addresses so what happens when you explicitly declare a variable you say i want to store a certain type in this variable where your computer creates a variable and stores it there and you'll see that it actually has a set of empty strings which i'll get to in a second because you've declared this variable you can now set it equal to a value for example you could set it equal to hello world and it replaces that empty string a variable with a value is the result of either using explicit declaration followed by setting it to a value which is two steps or you can do it all in one step which is implicit declaration now that you've set that variable equal to a string and given it a value you can actually change the value although you can't change the data type that's what it means to say that go is a statically typed language it means the data type cannot change once you declare a value to hold that type that means greeting cannot be set to any of these types because they aren't strings i mentioned earlier that when i first declared the greeting variable the variable held an empty string this is what's known as the zero default value type for a string and actually every value in go has a zero default value type let's look at the other values these are the default values for the corresponding data types this is really important to know for web development because it can often lead to unexpected values being stored in memory for example you can see that the default value for a boolean is false this means that simply creating a variable of type boolean will store a value of false at that memory location this is something to keep in mind at this point you understand primitives explicit and implicit declaration and default value types so this sets you up nicely for our next goal tutorial on pointers thanks for watching
Info
Channel: Esoteric Tech
Views: 1,448
Rating: undefined out of 5
Keywords: Go Tutorial, go data types, golang data types, golang variables, go variables, go declarations, golang, golang introduction, Golang Tutorial, Golang For Beginners, Golang Programming, golang programming language, golang tutorial for beginners, learn golang, golang beginner tutorial, go language introduction, golang tutorials, Golang SDK, Go Installation, programming tutorial, introduction to go, Learn go, golang installation, go programming tutorial for beginners
Id: zSYbvw2wgU0
Channel Id: undefined
Length: 12min 20sec (740 seconds)
Published: Fri Jan 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.