Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - SCOPE


'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.


Example :



package main
import "fmt"
    
func main() {
    
    myFunction()
    fmt.Println(x)			 	
}
    
    
func myFunction() {
    x := 5	
}


Output :



 undefined: x

So, in the first line itself, the Function 'myFunction()' is called.


myFunction()

And the function execution begins.


func myFunction() {
    x := 5	
}

And initialised the variable 'x' with '5' inside the function 'func()'.


x := 5

java_Collections

Then we come to the print statement,


fmt.Println(x)

And end up with an error as output,



Output :



 undefined: x

This is because the variable 'x' is not accessable outside the Function. And that is called as 'Local Scope'.


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()'.


Example :



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()")
}	


Output :



 The variable x : 5 is not accesible outside the firstFunction()
 The variable y : 7 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.


Local Scope in Nested Function


Nested Function is a Function inside an existing Function.


Example :



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()	
}


Output :



 The variable x is accesible from the outer_func() and the value is 5
 The variable x is accesible from the inner_func() and the value is 5

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.


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()	
}


Output :



 undefined: x

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'.


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 :


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)
}


Output :



 Inside the function 5
 Outside the function 5

So, in the above example, we have declared a variable 'x', outside the Function 'myFunction()' and 'main()' Function and initialised with '5'.


var x = 5

java_Collections

Note : Declaration of global variable should be using 'var' (i.e. 'var x = 5'). And not using ':=' (i.e. 'x := 5')

Now since, the variable 'x' is declared outside the Function 'myFunction()' and 'main()' Function, it is accessible both 'myFunction()' and 'main()' Function.