Golang Tutorial #2 - Variables & Data Types

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome back to the golang tutorial so in this video we're gonna be talking about variables and data types which in my opinion are some of the most fundamental and important aspects of any programming language now there's a ton of different types there's a ton of different ways to make variables and access them so I'm gonna be showing you the most common and most important ways here in the next video I'm gonna go into more detail so if you think I missed something hold off I probably cover it in the next video okay so what we need to do to start is understand what a variable actually is so in programming we have variables what a variable is is essentially some way of storing and accessing information so it's gonna hold some value for us we may manipulate that variable so change it later in the program we may use it somewhere we may pass it around but the idea is it's just kind of a slot that store some type of information so using the classic example from your math class you often saw variables like X right maybe so X Y maybe you even saw is Zed if you got crazy in three dimensions but we have X maybe you saw something like x equals five now if I write the equation X plus five you can tell me that this is ten because you know the value of x is five now this works very similar in programming except what's on the right side of this equal sign might not always be a number so we might have a string which is something like this right we might have a character we might have a byte we might have a floating-point number there's different things and what's on the right side is actually what's known as a type so we have some type that goes on the right side of our variable and that type is very important in a language like golang so if you've worked in languages I want to say like JavaScript or Python you may not be super familiar with the notion of statically typed languages what that means is once a variable is declared to be a specific type it cannot change so if I make the variable X and I say that that variable is of type number now that's not a real type but we'll talk about that in a second then it can never be of type string it has to be of type number all the time unless redefine that variable or we you know I guess remove it and recreate it or something like that but that's the idea in a statically typed language is types do not change whereas in a dynamically typed language guess what they change so that's the idea now to make a variable the syntax is var that's the VAR keyword the name of the variable which can be anything I'm going to talk more specifically about this pretty much anything you want but it does have to follow a few rules so it has to be comprised of only letters numbers and underscores those are the only three characters you're allowed to use it may not start with a number and it may not contain any special characters or any spaces so a valid variables name is something like Tim underscore name - that's fine time underscore name - it has an underscore it has a number it has letters that's okay a valid variable name is Tim a valid variable name is Tim underscore a valid variable name is Tim underscore one an invalid variable name is one Tim because it starts with a number an invalid variable name is Tim hello because it contains a space an invalid variable name is Tim slash hello because it contains a slash that's the idea just follow those few rules only numbers letters underscores no spaces cannot start with a number you'll be good to go all right also cannot I believe be a key word so I don't think I can name a variable import I think that will cause a problem so stick away or stay away from naming your variables anything you would typically see like func you know fmt main you probably don't want to call it those things or you run into issues so I'm just gonna make a variable here and I'm gonna call this name now what I want this to be is of type string now notice it's actually showing me the suggestion here already but what you do is you type a VAR the name of the variable the type of the variable which we'll get to in a second in this case I'm gonna type string you put an equal sign and then on the right hand side you put some value some text some number that corresponds with the type you picked so in this case I pick string so that means on the right hand side here I need some string so I need something inside of double quotation marks now it can be just the double quotation marks that is still stringless known as the empty string but I could do something like hello Tim and this would be Val I have now just created the variable name this is a type string and it stores the value hello Tim that means if I print out the value of name it will show me hello Tim because that's what it stores that's the basics with variables now there's a few other ways to do this in fact what we can actually do is say well I want to make a variable called name that is of type string but I don't know what value it's gonna be yet so what you can do is define that you want to make a variable called name of type string and later on in the program so below that line you can type something like name equals Tim and that's fine what that will say is now name is assigned the value of Tim and if you want to change this later on you can say name equals let's say Bill or something like that and that will change the value of name to be Bill that is the basics of how variable works they're not too confusing now there's a few other ways to define variables I'm gonna talk about that in the next video where I talk about implicit versus explicit declaration or assignment sorry but that that's the idea these are variables okay so now that hopefully we understand variables I'm going to show you it is a few different types that we can set for our variables because remember we have bar we have the name of the variable and then we have the type right that goes here so we need to know what types we can have and what those mean and why we would use them okay so the types so I actually wrote a little thing here just to show you I just stole this from the golang site we have numeric types we have kind of string types boolean types there's a bunch of different ones but we're gonna go through them one by one so the first type of types now I know this is weird but like I guess category of types we have are integers so these are some kind of integer values they can be unsigned signed or machine-dependent and let's go through all of them so the first type is you int 8 now that stands for unsigned integer with eight bits now what is a bit well a bit is simply a 0 or a 1 represented in the computer right so all your computers represent information in zeros or ones unless you're using a quantum computer I believe yes I think anyways in standard computer zeros and ones so with 8 bits otherwise known as one byte which is another way to refer to you into eight you can represent the values at 0 to 255 now the you extends for unsigned unsigned means this is not a negative value this is a positive value so with 8 bits or one byte one byte is eight bits that's what it means you can represent a number between 0 and 255 so if you want to have a small positive number you would use the type you int 8 okay you in 16 straightforward this is between 0 & 6 5 5 3 5 notice that all this has done is added 8 bits to our representations so how much space this variable is actually taking up which means now we can represent a much larger number which is actually 2 to the 15 minus 1 I think you're 2 to the 16 minus 1 like if you do that exponent I think that's what's 65535 is anyways it might be wrong about that but I'm pretty sure that's right okay so now we have you int 32 now again all we've done is we've added 16 more bits and now this is the number we can represent which is exponentially larger and we go down to you in 64 and same thing so these are the ranges I've written on the right notice that you int 8 and byte are the same thing so you can write either byte or you int 8 they mean the same thing okay now we come down to signed integers now signed integers are pretty much the exact same as unsigned integers except they can be negative and all that means is the first bit of our of our data now sorry or of the number is actually going to represent whether it's a positive or negative number so here notice that you int 8 represents from 0 to 255 now we represent from negative 128 to 127 so if you actually count the values that's the same number of numbers we can represent but the magnitude is different because we have the ability to represent negative numbers now so that first bit we need to use for either a negative sign or a positive sign I believe one means negative 0 means positive so notice that it is a hundred and I guess twenty eight numbers magnitude less right and that will be the same for all of these someone someone yes so now we'll go to int16 and notice that this again is smaller in magnitude than this but it represents the same values and that's because it's 16 we need that first bit in 32 this is known as rune so same thing you can represent in 32 with word rune represents these values in 64 represents these values so typically you don't need to use more than 32 or more than you at 32 now it's common to just represent 4 because our computers have so much memory in space now that it doesn't really matter what data types are using in terms of the magnitude of them unless you're writing a very memory efficient program but what to keep in mind here is if I write that my variable is of type u in 64 that means I'm gonna use 64 bits to represent it no matter how big it is so even if it's only the number one or its number zero we will still take up the same amount of space as a number that looks like this so that's why it's a good idea to use a smaller bit number so like you went eight if you're gonna represent smaller numbers because you will use up less space in the computer okay so now we're gonna go to machine dependent types so machine dependent types are just things that you can type and your computer will automatically kind of figure out how big these are supposed to be so if you type u int what this will do and I believe it actually looks at if your operating system is 32 or 64-bit it will use 32 bits or 64 bits to represent an unsigned integer so it's common that you'll see people just type u int or int as their type and that means that based on the OS they're using it will either be 64 bits or 32 bits so this is the exact same as u int it will be 32 or 64 and then you int PTR um you can read this definition here I'm not really gonna describe exactly what that is I feel like that's a little bit above and beyond the audience here okay so now we're on to floating point numbers so the types here are float 32 and float 64 now it's common just use float 64 but again this is the same thing so this means a floating-point number so some kind of decimal number with in this case 32 bits of precision so we're using ie E 754 if you know what that means great if you don't don't worry about it and this is a 32 bit precision number which means we're gonna use 32 bits to represent this float64 same thing here now float against tense or just a some decimal number like any if you add a decimal immediately becomes a float because we need to represent those decimals with some level of precision either 32 or 64 bits okay then we have complex types now complex types involve an imaginary part I'm not gonna talk about complex data types because we're probably never gonna use them in fact in this series I will tell you right now we're not gonna use these but if you're in physics are you doing some computational math there's some more advanced things you will likely need these complex sixty-four complex 128 so you can read through that definition but you know pretty straightforward strings as we talked about anything in double quotation marks I didn't add this in here but characters or anything in single quotation marks and then boolean values are either a true or a false so a boolean just tells us this is is true or it is false we're gonna use those a lot in this series and these take one bit so true is 1 and false is 0 but we just write true and false to represent that in our program alright so that's the basics of types and data types now let's just define a few variables of different types and then will pretty much be done with this this tutorial series so let's make some int let's say var number u int let's see EWP's you int equals and actually let's do 260 and see what happens so I'm gonna make this view into eight and we'll make this equal to 260 which is out of the range of you int right so look you int eight is only up to 255 let's save this and let's actually run this and see what happens so we noticed that I'm actually getting this squiggly value here and it says overflows so that's telling me that this here is too big to fit into eight bits so if I change this to 16 and we hit save no more errors we're good but wait what's it saying here number declared but not used so whenever we declare a variable in golang or in fact whenever we declare anything for that matter if we don't use it that's actually an error so if I don't use number in some capacity then it's action a problem we're not allowed to declare it unless we use it so actually let me show you how we can type or display this number so I believe I can actually just do comma and then put number here and now what's gonna happen is we're gonna print hello world and then we're gonna print this number 260 afterwards so let's open up that console if I can find this let's go a few terminal back to full screen unless you go run tutorial and see what happens okay so a hello world to 6 so that's the basics that's how you can print out the value of the variable is by doing 260 now I can actually go ahead and do something like number equals number plus 5 and we'll talk about arithmetic operations later but if I do that this will change the value of number to be 265 because we'll add 5 to it so let's save let's run this down here to enter and now let's see what we print out we get the value 265 so when we're printing things out with println we can separate things by commas and print multiple values we can print multiple variables if we want and just display things but I think that I'm gonna leave the tutorial here so that's a little bit about types and about variables hopefully this made sense I go very in detail with these things just because at the beginning I really want you to understand how how old this works and yeah with that being said I hope you enjoyed it if you did make sure you leave a like subscribe and I will see you in the next goal ink tutorial
Info
Channel: Tech With Tim
Views: 47,336
Rating: undefined out of 5
Keywords: tech with tim, golang tutorial, golang variables, golang data types, data types golang, go tutorial, go programming language, golang for beginners, go tutorial 2020
Id: pM0-CMysa_M
Channel Id: undefined
Length: 14min 46sec (886 seconds)
Published: Sat May 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.