Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 Variables and Types

Like most other languages before it, Go is built on a foundation of basic data types that you can use throughout your applications. The list of basic built-in types is fairly small, and it’s easy to get familiar with them all. That’s just what we’ll do in this lesson.

Related Links

2.1 Variables and Types

One of the most important and fundamental things about any programming language that you're gonna typically need to understand, and in this case Go is no different. Is we're gonna need to know how to work with different types of data within the Go Programming language and how to store those things in variables. So this is a foundational thing, like I said, in any sort of programming language. But in Go, there's a couple little interesting idiosyncrasies if you're not used to the Go Programming language. Or if this is the first time that you've ever done it, things might seem a little bit strange. So as you can see here I have a variable's dot go source file here, and I have my typical skeleton that I mentioned before that we're gonna see very often. So the first thing that I wanna do is I wanna show you how to declare a very simple variable and how there are many different ways to do that. And then we'll talk a little bit about the different types that are supported out of the box. So the first thing that I wanna do is I want to create a variable. So in my main function here I wanna do is I want to, say, create a way to store my name. So typically in most other programming languages, and once again Go is no different, I wanna create a string. Now as I mentioned before, there are a number of different ways to do that and we're gonna start with one of the most basic. So we're gonna start with the var keyword, and you're gonna see that that's one of the ways that you can create or declare a variable. But there's a number of different ways to do this. So the first thing that I wanna do is I'm gonna create a variable called name, and I'm gonna set this equal to string. Or in this case I'm gonna set it to a string, Derek. Now this is the first way that we can declare a variable called name and then also initialize it to a value. Now I don't have to initialize it to a value but Go is smart enough to figure out what the type is from type inference so in this case it's gonna know that Derek is the name. So if I go ahead and change this to say I wanna print out a name and I can go ahead and save that, then I will specify go and run. So I can kind of bypass just running the build and then having to manually execute my source file. I can simply say go run. And I can say Variables.go and it's gonna build, compile and run this. So you're gonna see, it's gonna give me the name Derek. And that's probably not too crazy, you've probably seen similar things to this before. But where things start to stray a little bit from maybe other types of languages is how we specify the types. So let's say we didn't initialize it to something and we wanted to specify this as being a string just through declaration. The way that we would typically do that within Go is going to be saying var name and then specifying the type after the name declaration. Now if this is valid, we can definitely save our name string, and if I were to try to run this again you're gonna see it looks like I get no output. But in actuality this is an empty string down here. And we'll talk about some of the default values for the different types. It just so happens that the default value for a string is not null as you might think of in other languages but actually the empty string. So I've now showed you two ways to declare. So I've declared my name using type inference and using the initialization or just this way. So now at this point I can now assign a value. I could say my name is now Derek and I can save that. And go ahead and run it, you're gonna see now I get Derek again. So that's one of the basic ways to declare a variable using both type inference and, or as an explicit type. So as you can see, string is one of the base types. Then we also have a number of different numeric types. We have integers, we have unsigned integers, floats, and complex numbers. And there's a several different variations of all of these. So we are able to declare them explicitly once again as an int8, an int16, an int32, and int64. Now, you've probably seen this in other languages before and the numbers after the int simply specify the size of that integer. So in other programming languages, you can also do that as well. You can specify the size but it's highly recommended when it comes to any of the numeric values or explicitly int and unsigned integer, that you just use either int or uint. And the reason for this is because Go is optimized to be able to kind of figure out what is the best case and what the size should be. And the only way that that happens is if you specify int or uint. As soon as you start to put sizes at the end of those types, you're gonna be forced into using that size and the best way, maybe the most platform agnostic way to handle that, is simply using int and uint. So I can create integer variables in a similar way that I did before. I can say var age is equal to, say, 20. And this is gonna figure out that this is gonna be an integer. And once again I could do the same thing where I could be explicit about it and I can say this is gonna be an int. Or I could say this is going to be a uint and or things like that. Very similarly to how we did with the string value of name, and I could once again print this out. I could say I want to print age this time. So let's go ahead and save that. And we'll go ahead and run it. And this is one of those things that I mentioned before, where a lot of programming languages are a little bit more loose, even though they are statically typed in the fact that you can do things that are a little bit sloppy and they'll just be kind of warnings in the compiler. So say a language like Java or C#, if I were to declare a variable and not use it within my application, you might just get some warnings or it might just say, hey, by the way you declared this but you're not using it, but that's okay. But as you can see here, Go is not that loosey goosey. Go sees the fact that I created and declared a name variable and it's not used and because of that this is now a nonfunctioning program. It did not build. So in order for me to fix that I'm gonna have to get rid of those lines and I can save this. And if I were to come back in and run this again it's gonna work now and it's going to print out age. So that's one of those things you definitely have to be careful of that might be a little bit confusing if you're just coming to Go from other languages where it's a little bit more flexible when it comes to those types of things. So now another way that we can create a variable and another type that we can use within the Go programming language is a boolean or a bool. So this is gonna be either your true or false value. So we can continue to declare variables using the var keyword or things like that. But another way to do it is using another shorthand inferred type mechanism for these variables. And the way that we do that is going to be using a special shorthand syntax, where I provide a name. So in this case, we'll call this a bool. Just so we have something to name it. And then we're gonna use this colon equals syntax, which is gonna be this short hand notation to say to the Go compiler, hey, I'm creating a variable, my variable name is on the left hand side. And then I'm going to need to explicitly use type inference to figure this out. And in this case I'm gonna default it to true. So now I can save that, once again it's can you can see that I have a Linter install that's gonna give me these save time warnings, these develop time warnings that's going to say hey, you should do this otherwise it's going to not build. So in this case I have to use it, so I'll say a bool and we'll save that. And I'll go ahead and run this command. And you're gonna see that it spits out true. So now we've talked about strings. We've talked about integers or numeric type values. And we've also talked about booleans. And those are gonna be the basic built-in types that we're gonna wind up using quite a bit throughout our applications. Now there are other types that are referred to as aliases. We're gonna use this as we get into the kind of web application development aspect of this course. And I'm gonna show you that right now, and one of those is a byte. A byte is a built-in type within the Go programming language but interestingly enough it's actually An alias to a you int eight. So while we can use you a uint8 byte, is kind of an alias for that. And usually that makes a little bit more sense to people who have more of a background within computer science. And they understand what a byte is, so you can usually use either one of those depending on whatever your preferences either way should be fine. Now another interesting thing about variables and initializing them within the world of Go is this concept of multi initialization. So I can, in a single line, or inline, I can declare and or initialize a bunch of different variables all at the same time. Which, if you've never seen this before, it can get a little bit confusing. But I'll show you how it works. So let's go ahead and create a couple different variables. So I'm gonna use the var keyword and I'm gonna say that this is going to be good is my first variable, bad is my second variable, and something is gonna be my third variable. And I want to initialize all of these together to some list of values. So good, of course I'm good. Bad, no I think I'm pretty good, so I'm gonna say that bad is false and something is going to be some string and we'll just call this else, like that. So now I have created three variables in one line, good, bad and something. And I have initialized them all together in the same line as well, all comma separated. So this all is an accepted way to create a list of a bunch of different variables, all at the same time and initialize all of them. So there you go. We have now gone over the basic process of creating variables and the basic types that you're gonna find within the Go Programming language. Once again, a boolean string, integers, unsigned integers all of different sizes, and just to be clear, there are a couple different numeric values that are floats. A float 32 and a float 64, which give you a little different level of precision when it comes to floating point numbers. There is no generic float. So if you need to use a float you need to be explicit with the size, either 32 or 64. And as I mentioned before there's also complex. Now complex is gonna give you those complex values that have a real aspect of the number and an imaginary aspect of the number. So if you're into mathematical computation and you use complex numbers, then there is support for that within the Go Programming language as well. And in this case, once again, there's not a generic complex but there's a complex 64 and a complex 128. So if you needed to use those types of values within your application, you can absolutely use those types. So now we have a basic understanding of the basic types and how to declare variables within Go.

Back to the top