Welcome, in this lecture I'm going to talk about the variable declaration syntax.
For example to declare a variable you need to follow
the rules of the variable declaration syntax of Go.
Also you need to declare a variable before you can use it.
So you can't just throw a value to an unknown variable that doesn't exist yet.
This is important and necessary for the compile time safety.
For example in a dynamic programming language you can just assign a value to a variable even before
declaring it. It is easier to use
but it may not be easy for you to maintain your program afterwards.
Let's start with an example.
Imagine that you have a customer who wants you to write a program that will record various measurements
from vehicles. In this imaginary scenario
your program will be installed in two millions of vehicles.
Oh my goodness.
OK, then you go and start programming.
Here is the usual package main and main function as an entry point to our program.
I'm going to declare a variable for storing the current speed of a car, like this.
Let's zoom a little bit into this declaration to understand it better.
You might have already guessed that this is the variable declaration syntax in Go.
Now let's take a look at the parts of this syntax.
This "var" keyword means
variable. It simply tells Go that let's declare a variable.
So you always have to type this keyword first. After the "var" keyword, you need to give a unique name to your variable.
Here the speed is the name of this variable.
More formally, names are also called identifiers. A name or an identifier helps
you and Go to understand each variable you are referring to.
And as I said before a variable gives a name to a value,
so you can reuse it later.
Naming your variable is such an important concept that there is a saying that goes like this.
There are only two hard things in computer science.
Cache invalidation and naming things.
And I agree with that.
I'll talk about a few tips about selecting good names later.
Let's take a look at a few rules about names.
By the way, keep in mind that these naming rules are not only for variables, they also apply to other
names such as function names, package names and so on. OK.
The first rule is that a name should always start with a letter or an underscore character.
This is because, as you've seen before, when the first letter of a name is an uppercase letter that name
becomes exported so that other packages can use it.
That's one of the reasons why a name should start with a letter.
OK, let's take a look at a few more examples.
A name can contain uppercase letters in this example,
this SpeeD name is exported since its first letter is an uppercase letter.
It also ends an upper case letter.
However I use it here as an example.
Normally we don't name variables like this. As an exception a name can also start with an underscore
character.
And lastly a name can have Unicode letters. By the way, this word means speed in Japanese.
I hope that is true. This works because Go can understand Unicode letters.
Now let's take a look at what you can't do.
For example a name cannot start with a number like this, or a name can't start with a punctuation character
like this.
Also a name can't contain a punctuation character inside or it can't end with it. It's because punctuation
characters have special meanings in Go
and it is easier for Go compilers to parse the names.
And lastly, a name can't only contain reserved Go keywords.
For example if Go was allowing you to do that, you'll never be able to declare a variable since the var
keyword declare a new variable. So Go doesn't allow you to override its own keywords. As you've seen
these rules are there to prevent ambiguitys. They help Go compiler to understand what you mean and they
make the compiler's job easier.
OK, that's all for the naming rules.
Let's continue. Here int is a type of this variable. It is short for integer. In Go every variable and value has
a type. It's because Go is a strongly typed programming language.
It's not like Python or Javascript.
It's more like C, Java, and C-Sharp. This helps the compiler to find bugs in your program even before you run
it.
A variable's type determines what type of values you can store in the variable, and where you can use that
variable.
For example this is an integer variable, right?
So you can only store an integer value in this variable, and you can only use it with other integer
values or variables.
So as I said in the introduction, once you declare a type for a variable
you can't change it afterwards.
It's static.
If you could do so then there wouldn't be this strong type safety of Go. That would invite bugs.
OK, so now you know what the variable declaration syntax looks like in Go.
Let's get back to the example program.
Now let's print this variable to the console. Let me type Println.
All right, ready.
Let's run it now.
It prints 0.
It's because a variable is initialized to a zero value when it's been declared
without an initial value.
I'll explain what a zero-value means later.
Now let's take a look at what happens when you use a variable before you declare it. In Go the order
statements are very very important. Since Go runs the code from top to bottom.
Here, I have moved the variable declaration after the Println call. Can you guess what would happen now?
OK let's run it.
This program cannot even compile, let alone run! The error message says that the speed variable hasn't been declared.
This means that it doesn't exist yet.
So Println can't print it.
If you're an experienced programmer this is not a surprise for you.
However if you are coming from a language such as Javascript then you know the difference. If you don't
know Javascript let me tell you that
in Javascript you can do this.
I mean you can use a variable even before you declare it.
It even has a name: variable hosting.
Check out the link in the resources.
You will understand what I mean.
I have prepared a program for you in Javascript.
So in the end the speed variable looks like this.
Its name is speed, it's static type is int, and Go has assigned it a value of zero automatically. And the type
of its value is also int. So congrats,
Now we know how to declare a variable. By the way
I know that this was a lot of talk but don't worry you'll see more examples as you progress.
So you'll understand everything
naturally. However this also depends on you.
You can always pause the videos and type the programs along with me, so you can try them yourself.
It is the best way to understand Go.
All right thank you for watching.
See you in the next lecture.