'Scope' can be defined as the accessibility of a variable.
Say for example, in every office, there is a restricted area where only a few people have access.
Similarly, in Go, the variables also have some access restrictions. The variables that are defined inside a Function, cannot be accessed outside of it.
package main import "fmt" func main() { myFunction() fmt.Println(x) } func myFunction() { x := 5 }
So, in the first line itself, the Function 'myFunction()' is called.
And the function execution begins.
func myFunction() { x := 5 }
And initialised the variable 'x' with '5' inside the function 'func()'.
Then we come to the print statement,
And end up with an error as output,
This is because the variable 'x' is not accessable outside the Function. And that is called as 'Local Scope'.
The variables that are declared inside a Function, can only be accessed inside the Function and is local to that Function. That is called the 'Local Scope' of a variable.
Say for example, if there are two functions, 'first_func()' and 'second_func()'.
package main import "fmt" func main() { firstFunction() secondFunction() } func firstFunction() { x := 5 fmt.Println("The variable x :",x, "is not accesible outside the firstFunction()") } func secondFunction() { y := 7 fmt.Println("The variable y :",y, "is not accesible outside the secondFunction()") }
So, we have declared two functions, 'firstFunction()',
func firstFunction() { x := 5 fmt.Println("The variable x :",x, "is not accesible outside the firstFunction()") }
And 'second_func()'.
func secondFunction() { y := 7 fmt.Println("The variable y :",y, "is not accesible outside the secondFunction()") }
And we have two variables, 'x' and 'y'.
While 'x' is declared inside the Function 'firstFunction()' and is local to the 'firstFunction()' and cannot be accessed outside it.
Similarly, 'y' is declared inside the Function 'secondFunction()' and is local to the 'secondFunction()' and cannot be accessed outside it.
Next, let us see the Local Scope in nested Function.
Nested Function is a Function inside an existing Function.
package main import "fmt" func main() { outerFunction() } func outerFunction() { x := 5 fmt.Println("The variable x is accesible from the outerFunction() and the value is ", x) innerFunction := func() { fmt.Println("The variable x is accesible from the innerFunction() and the value is ", x) } innerFunction() }
So, in the above example, there are two Functions, 'outerFunction()' and 'innerFunction()'. And the 'innerFunction()' resides inside the 'outerFunction()'.
Now, we have declared a variable 'x' inside the 'outerFunction()'.
func outerFunction() { x := 5 fmt.Println("The variable x is accesible from the outerFunction() and the value is ", x) innerFunction := func() { fmt.Println("The variable x is accesible from the innerFunction() and the value is ", x) } innerFunction() }
Now, since the 'innerFunction()' resides inside the 'outerFunction()'. The variable 'x' is also accessable inside the 'innerFunction()'.
Now, what if, the variable 'x' was defined inside the 'innerFunction()'. Can the variable 'x' be accessed from the 'outerFunction()'?
Well! The answer is no.
Let us see the above example.
package main import "fmt" func main() { outerFunction() } func outerFunction() { fmt.Println("The variable x is accesible from the outerFunction() and the value is ", x) innerFunction := func() { x := 5 fmt.Println("The variable x is accesible from the innerFunction() and the value is ", x) } innerFunction() }
So, in the above code, we have declared the variable 'x' inside the 'inner_func()'.
func outerFunction() { fmt.Println("The variable x is accesible from the outerFunction() and the value is ", x) innerFunction := func() { x := 5 fmt.Println("The variable x is accesible from the innerFunction() and the value is ", x) } innerFunction() }
So, the variable 'x' is not accesible from the 'outerFunction()'.
So, the moral of the story is, the variable that is declared at a higher scope could be accessed at a lower scope.
Say for example, your Boss will have access to your details. But you will not have access to any of your Boss's details.
And with this we come to the concept of 'Global Scope'.
The variables that are declared outside the Function are accessable inside a Function. And they are said to be in 'Global Scope'.
Let us see in the below example :
package main import "fmt" var x = 5 func main() { myFunction() fmt.Println("Outside the function ",x) } func myFunction() { fmt.Println("Inside the function ",x) }
So, in the above example, we have declared a variable 'x', outside the Function 'myFunction()' and 'main()' Function and initialised with '5'.
Now since, the variable 'x' is declared outside the Function 'myFunction()' and 'main()' Function, it is accessible both 'myFunction()' and 'main()' Function.