Welcome! In this lecture, I'm going to show you when to use a short-variable declaration.
I've decided to add the lecture about this since I've seen a lot of people curious about it.
OK, let's get started!
Here are a few tips for you about when to use a variable declaration
instead of a short declaration.
Here's the first tip: If you don't know the initial value for a variable then use the (normal) declaration.
This is a convention among Go community.
I recommend you to follow it. So that everyone can understand your code more easily. By the way,
when I say "declaration", I mean the (normal) variable declaration.
So, let me show you an example for this in the editor. Let's say that you want to track game scores
and you don't know the initial score.
To do that, don't use a short declaration here. For example,
this is not good. Instead, it's better to use a declaration here.
You can do so like this.
This will be initialized to its zero value automatically which is zero. So, others can know understand
that you don't know this variable's
initial value by looking at this declaration.
Here's the second tip.
Use the normal declaration or variable declaration when you need a package scoped variable.
For example, here, at the package scope, you can only use a (normal) declaration, you can't use a short declaration.
For example, you can't use this.
But you can do it with a declaration like this. Alright.
So, here's the third tip: Use the declaration
when you want to group variables together for greater readability.
So, if you want to show other developers that some variables are related to each other, if they go hand in
hand, then use the normal declaration to show that to them (other developers).
Let's say that you have created a video player.
You can do that using a multiple variable declaration like this.
Let's declare a string variable named video.
This is the name of the video. And, let's declare two int variables named duration and current.
Note that, I have separated the duration and current variables from the video variable. So that, I'm
being grouping them here for better readability. Since the duration and current are related.
duration is the duration of the video and, the current is the current second a playing video.
Here are a few other tips for you when to use a short variable declaration.
Let me start by telling you that short declaration is the most used and preferred declaration style in Go.
As gophers, we like to write concise code.
So we prefer a short declaration over the variable declaration most of the time.
Here is the first tip.
This is the opposite of the declaration advice. If you know the initial value of a variable, then
use a short declaration.
Let's say that you want to calculate the area of a room and you know it's a width and height already.
Then, don't declare it like this.
Instead, use a short declaration like this.
OK.
And here is the second tip.
As I've already demonstrated to you in the previous example,
use the short declaration
when you want to keep the code concise and easy to read.
Here is the last tip. As you've seen, only with a short declaration, you can both declare and assign
to variables in the same declaration.
Let's say that you want to change the width and declare a new variable named color.
You can do so like this.
This first assigns 50 to the width variable.
So it changes its value to 50. And, here it declares a new variable named color with an initial value
of red.
But this is verbose and it's not simple.
Let's do the same thing using a re-declaration.
I'm going to comment out those first. Then, I'm going to use this redeclaration (statement) instead.
This will change the width to 50 and at the same time, it will declare the color variable with
an initial value of red. Looks better, right? As you'll learn later that redeclaration both a gift
and it's also a curse. It's because sometimes shadowing the already declared variables an be a nightmare.
Check out the link in the resources to see what shadowing means.
I've created an example code for you there.
I'll also talk about the shadowing in upcoming sections.
Also, as you'll learn later that short declaration has very useful properties. For example, it can be used
inside if and switch statements to create variables that belong to those statements only.
That's all! Well done. You've learned everything about declaring variables in Go.
See you in the next lecture.