Check out a free preview of the full Basics of Go course

The "Variables, Types & Const" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano explains the syntax for defining variables, constants, and data types in Go. The discussion also highlights Go's capability to infer data types and introduces a shortcut for defining variables.

Preview
Close

Transcript from the "Variables, Types & Const" Lesson

[00:00:00]
>> So variables. Let's get into the fancy part. So you create variables with bar. It's bar, space, the identifier of the variable. So in this case it's x, so bar x, a space and the data type. No colon, it's a space, and it goes after, not before the identifier.

[00:00:24]
You need to get used to that, but yeah, it's cool. You say why is not, there is no colon? Simplicity, let's avoid the shift tab in a key. Simplicity, let's make it simpler, okay? So it's a space. So here I have x as an integer, I have a name as a string, and we use the bar keyword.

[00:00:47]
That's the only keyword we are going to use for variables. Then we have constants that is similar, const, the identifier, and we need a type. And we always need a value, why? Because in go, we have only constants. And I will explain what that is in a minute.

[00:01:08]
Also, if a variable has no value, for example, in this case x and name and not saying equals something, they have nil by default. Nil, it was, you know as null on other languages. Here it's called nil. Okay, for now, let's say that. Constants can only be bool, strings or numbers.

[00:01:29]
Nothing else because here they are real constants. For example, in JavaScript when you use a const keyword, you don't get constants. You get immutable variables. Does anyone know the difference between a constant and an immutable variable? Have you ever heard about those difference? So let's start with an immutable variable.

[00:01:57]
An immutable variable is a variable, which means it's an a space in memory. So it's a memory that needs to be allocated. Immutable means that there is an engine that is checking that no one is changing the value. So once it was set, no one can change the value.

[00:02:11]
That's an immutable variable. In JavaScript that's the case. So it's an immutable variable. So it's in spacing memory and you can assign, because it's a spacing memory. You can assign values from the user's input, values that it's coming from a JSON from the server. It's a memory. We allocate the memory, we put the value, and we check that no one is changing that.

[00:02:33]
That check system uses runtime execution. So that's against our go's. So we don't have immutable variables here. We do have constants. And what's a constant? Quick and dirty, the compiler will kind of copy and paste. So when the compiler finds a constant reference, it goes to the definition copy the value and paste the value.

[00:02:57]
So it's kind of a shortcut to copy and paste. I'm simplifying the definition. But in this case, it's not that it's not in memory, the constant. It's just a value, a fixed value. That's the difference. With a string, there is an exception to that. But that's the idea, right?

[00:03:14]
It's actually a fixed value and on most of the cases it's not even in memory, yeah
>> So for a constant you don't have to define the type?
>> You can, but if you always need a value. Something that we can do and we are going to see that also with bar.

[00:03:35]
Is that if you're assigning a value, on some cases, you can get an implicit type declaration.
>> Yeah, that makes sense.
>> But for constants, in fact, if you're used to JavaScript, and in JavaScript today, we use constants for everything. So constant is default and if on some cases we use sled in JavaScript here constant are for various specific things but you're not going to use const a lot and go just for constants.

[00:04:11]
Values, URLs can be domains, things that you know, they won't change after compilation, because the constant value must be set at compilation time. And when it's an immutable variable, you can set the value later after compilation or runtime. A constant cannot set its value in runtime. That's the difference.

[00:04:36]
So, we can create variables initializing the value at the same time with equals. So the equals is the assignment. Strings will use double quotes, okay, which is, so you don't have single quotes. We do have a backtick as well as an alternative but it's just the same. So it's not like the backtick we have like templates as in JavaScript.

[00:05:07]
Also there is something known as variable initialization shortcut that it's using colon equals. Colon equals will create the variable for you. So we will use this a lot today. So you will see this in action. And so we don't need to add the bar keyword in that case.

[00:05:29]
And we don't need to set the type. It's when you wanna create the variable and at the same time set the value. For example, you wanna make these two lines shorter. So instead of creating a string variable, and then setting a value, you can use directly these syntax.

[00:05:46]
This syntax works only within functions. You cannot use it as a global variable. Okay, we will see that. That syntax that is colon equals for me it's a trip in time actually to my first language. So I have a start in doing development coding with Pascal, Aturo Pascal.

[00:06:09]
And Pascal that was the way to assign values to a variable. So for me is like coming back to when I was a teenager, and I was coding as a teenager. Okay, so that's how you define barbers. Do we have anything else to cover? No, that's all. Remember, this is actually simple.

[00:06:31]
Now let's go back to our module to play with this a little bit. So for example, if I want to create a variable for this text. We use bar the message, the identifier and the type. In this case it's a string, s. You will see that the ghost compiler so the mix between the compiler and the linter.

[00:06:57]
So both are working together here, they have anxiety. They suffer from anxiety. So you will see a lot of error messages immediately after creating a variable. And that's because you haven't used the variable, okay? And at one point, it will stop your flow of thoughts at first because they come on, what's going on?

[00:07:15]
I have an error. Well, you didn't use the variable. Yes, I have just typed it. It's like gimme a break, okay? But you will see that a lot. Okay, it's really a common pattern when you start working with Go that you feel that kind of weird, it's like, gimme a break.

[00:07:31]
I have just created the variable. I know I didn't use the variable. So that's why when you're still thinking what you're going to do, if you wanna get rid quickly of that error message. Most of the time you have at the end of the function, a print that you will delete later, where you just print all your variables.

[00:07:49]
So because you're using the variable now, the linter is not complaining anymore. Of course, you can disable the linter as well, but that's not a good idea. Okay, because we wanna keep it. So in this case, I can create a number, a price. Okay, the same, it's complaining.

[00:08:06]
Why is it complaining? Because I'm not using it. And by the way, print accepts a dynamic amount of variables so I can actually use comma and send more than one variable at the same time. I can reuse the same print and print many things. So what is the value for the message?

[00:08:24]
In this case it will be an empty string. It's going to be nil. So it's an empty string, actually. Of course, I can set the value by default like Hello from Go, and if you run this, you'll see Hello from Go and blah, blah, blah, blah, blah. That's actually a float.

[00:08:45]
We will see how to format that later. We'll talk about data types in a minute as well, but you can see that on price I didn't set the type. Can I? Yes, I can. Just spoiler it's float32, for example or 64. But I don't need to set the type.

[00:09:04]
If I have an equals in the same line, the same here as a string, because he will implicitly get the type from the value, from the literal value that I'm using here. Okay, so for those of you that were kind of afraid of data types, well, you don't actually need to explicitly set the type all the time.

[00:09:26]
Sometimes CI will be necessary, but sometimes it won't. In this case, I might not need the bar. I can use directly. Colon equals the shortcut. But what's the difference? It's just creating the variable for you. That's all. And you use that a lot. In Go we use that a lot.

[00:09:49]
So you don't need to set the variable because when you set the variable, it first creates a variable with zero, with nil. And then later you set the value. In this case, you create the variable and set its initial content in the same line and for that we use the shortcut.

[00:10:06]
Okay, that's all. And for constants you cannot use the shortcut, so you must use the const. So then you say pi, something like that. If you want, you can set the datatype or not. Why typically you set the type when the precision that you get, the implicit definition, the implicit query of the type is not the one you want.

[00:10:36]
For example, I can set a number here, 34, okay? And what's the type of pi? I can try to look at it here. It says it's an integer and well maybe let's say instead of pi let's say maxSpeed. We will talk about the styling guidelines for naming variables in a second.

[00:10:59]
But let's say maxSpeed, 60, 60 miles per second. And it's a constant, of course, and it's an integer, which it might be what you want, but maybe you can get less, because an integer has a lot of bits available. It can go up to 16 billions, something like that.

[00:11:21]
And maybe you said, you know what, maybe I want just a byte. So then you can express here a different type. We don't know what the byte is, yes, but you can get an idea. And then yeah, because we know that the maximum speed at least we are talking about cars and highways and not SpaceX rockets.

[00:11:39]
Yeah, a byte is good enough. So then you explicitly put a type, because if not, it will get integer, okay? Only on those special cases where you want to add more metadata, you express the type, yeah.
>> If you wanted to use another shortcut, could you not put a type but then put 60.00 or whatever and then it would know?

[00:12:02]
>> Yeah, so what happens if I put .0? What happens with the type? Now it's a float. Now it's float. So yeah, I did a .0. We'll add that. In fact you might not need the 0. The problem is that we have a yellow. The yellow is a warning, so it will compile, but it says, here it says you're not using max speed.

[00:12:23]
I say, okay, I know. Okay, so now I'm using. You don't even need the 0. You can say 60., and the last dot will tell the compiler that it's actually a floating value, so it's a real number, another integer number, okay? But yeah, that's true. So also we mentioned that we have, so I'm not sure if you remember, but we mentioned that the go file can include variables, functions and type declarations.

[00:12:53]
This is a function, and then within the function we have variables. Also we can create some kind of global variables. So in this case we create the variable here. And our URL equals to frontendmasters.com. This is kind of a global variable, global to whom? Well we'll see. It's a package that's the spoiler, but we don't yet understand what the package is, okay?

[00:13:20]
Why it's complaining? Because I'm not using it. And probably you're thinking, why is so annoying with that thing? Well, the idea of simple code. So we shouldn't keep orphaned variables, variables that we said few days back and we're not using anymore that the Go will tell you, no, it shouldn't be there.

[00:13:44]
So remove that because that's making our code dirtier. So we don't want that, okay? So that's why it's so annoying at first. Okay, makes sense? Actually, that's all for variables that we can try it right now. It's actually pretty simple. We don't have a lot of other situations.

[00:14:06]
Have in mind in terms of variable scopes. The scope of a variable can be global. So this is a global variable. It can be function. So this is a function-scoped variables. It can also be block based variable. We haven't seen any block yet. It can be an if, a for, maybe, a while.

[00:14:29]
We'll see. But so when you open a code block and you create the variable within that code block, okay? It's block scope by default.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now