Hello and welcome to the Golang programming language course.
In the previous session, we talked about shorthand variable declaration, and in this session we want
to talk about the other variable concept.
It's corp of golden variables, this core public variable can be defined as a part of the program where
a particular variable is accessible, a variable can be defined in the class method loop or etc..
Like C or C++ and Go Lang, all identifiers are lexical is scoped.
The scope of a variable can be determined at compile time or, you can say, a value that can only be
called from within the block of code in which it is defined.
Goal and scope rules of variables can be divided into two categories, which depend on where the variables
are declared.
First, local variables declared inside the block or a function, and second global variables declared
outside a block or a function go.
Lang uses Lexicon, a scoping based on CodeBlocks to determine the scope of variable.
Inner block can access its outer black defined variables, but outer cannot access even a black defined
area of us.
Now it's time to go to VSCO and create a sample project.
Local variables features first variables that are declared inside the function or a block are termed
as local variables.
These are not accessible outside the function or large second.
These variables can also be declared inside the food virus or inside the function.
Third, these variables can be accessed by the master code blocks inside the function force.
These variables are also termed as the blood variables.
Fifth, there will be a compile time error if these variables are declared twice with the same name
in the same scope.
Six.
These variables doesn't exist after the functions.
Execution is over seven.
The variable, which is declared outside the loop, is also accessible within the nested loops.
It means a global variable will be accessible to the methods in all loops.
The local variable will be accessible to look and function inside that function and AIDS.
A variable which is declared inside the loop body will not be visible to the outside of loop.
But now let's do an example.
First, define two variables like
that variable.
One common granny able to bind to tight end equals 100 and 200.
If if the value of value build one color person the variable one and go to the next line.
If the value of variability to person.
The
variable to set the back is less now
and by just this.
Now reformat the code, save the project and execute the program, go to Terminal, Go to Project Folder
City Valuables.
You're a screen and go round dot back listening, let me go now we can see the output, the value of
valuable one is 100 and the value of very able to use 200.
Now we could define local variables in the main function, their scope of access to this variable at
this task with open curly braces and ends with closing.
Now, if you want to define about global variables, features first.
The variables which are defined outside of a function or a block is termed as global variables.
Second, these are available throughout the lifetime of the program.
Third, these are declared at the top of the program outside all of the functions or blocks and force.
These can be accessed from any portion of the program.
Now, let's go to do an example about global variables.
So first, clear the last code.
OK.
After the package name and in top of all, function, first we defined a variable named global variable.
So we write that global variable by the Typekit and initialize it by 100 value.
This is a global variable declaration.
Now go to the main function and define a local variable, for example, the low code that you know,
maybe the Typekit and initialize it by 200 display the value of global variable at half the value.
Are they, Lubi, by value to the Colin person, the
newborn value and go to the next line and now display the value of below par value of Earth Earth?
The value of local valuable column first in the local variable.
Reformat the court and say the project in order to better understand the concept of low cost variables
and global variables, we define and order function like display, so we defined a function like display
find.
Display.
Here we can use strong global variable, but don't use of local variables of other functions, and we
do not access them for the example.
Come here and display the value of global variable.
So have you right?
If if the value of global variable Colin first and the bank end and set variable blue by
now calling display function in main function.
Display before Manticore, save the project and execute the program first clear screen and run the project
by the comment goal, one got back as Bush made that go.
Now we can see the output first.
He could display the local and global variable values in the main function and then calling the global
variable in disbelief function and calling display function the main function and execute it.
And he could display all the local and global variables value.
In this case, we want to define a challenge.
So first, ask a question.
What will happen during a local variable with the same name as the of the global variable inside the
function?
The answer is simple, i.e. compiler will give preference to the local variable usually went to variable
with the same name is defined.
Then the compiler produces a compile time or modify.
The variables are defined in different scopes.
Then the compiler allows it whenever there is a local variable defined with the same name as that of
the global variable, then the compiler will give precedence to the low code variable.
We defined another program, and you can clearly see the output so clear the last code.
First, define the global variable declarations, for example, that validate those won by the tight
end, initialize it by, for example, five hundred now go to main function.
And now we create a local variable inside the main function that it is same as global variable.
Like that variable won by the tried and initialize it by 200.
The names of both variables are the same, and it's variable one with the difference that one of them
is global variable and the other is local value.
Now display the value off value of it.
If if.
The value of Gladiator one Collins per cent de facto exclusion and said that he, the one before the
court, saved the project and executed the program.
Now go to Termina first floor the screen and run the project by go round like a Scotch man that go.
Now we can see the output, the value of variable bond is 200.
So we see that the local variable is pretty friends to the global variables.
OK.
We have reached the end of this session.
I hope you take full advantage of this decision until next session.
Goodbye.