Golang program to show scope of variables


In this article, we will write Go language programs to show scope of global and local variables using pointer approach. Scope is referred to as the accessibility of the piece of code in the program in particular function.

Scope can be defined in two ways: globally and locally. Global means a variable declared in the main cannot be overwritten by any modification and remains same everywhere whereas local access means when variable is modified inside the function, it can only be used inside the function and its scope remains in that function only.

Using Same Global and Local Variables

In this method, we will write a Golang program to depict the scope of variables using one global variable and one local variable having same name.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output.

  • Step 2 − Create a main function and in that function create a global variable name and assign it with value

  • Step 3 − Print the name set globally on the console

  • Step 4 − Call the function named change_name and in that function chage the global variable to different name as set before

  • Step 5 − Print the different name on the console and also the name after the function is called using Println function from the fmt package where ln means new line

Example

The following example will demonstrate how to create a Golang program to show scope of variables using same global and local variables

package main

import "fmt"

func main() {
	
   name := "Veronica"
   fmt.Println("Name before function call:", name)	
   change_name()
   fmt.Println("Name after function call:", name)
}

func change_name() {	
   name := "Virushi"
   fmt.Println("Name inside function:", name)  
}

Output

Name before function call: Veronica
Name inside function: Virushi
Name after function call: Veronica

Using Pointer Approach

In this particular method, we will write a Golang program to show scope of variables using pointers. A variable named a will be created and assigned a value that will be incremented later.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output

  • Step 2 − Create a variable named a and assign it value inside the main function

  • Step 3 − Print the value of a using Println function from the fmt package where ln means new line

  • Step 4 − Call the function increment in the main with a pointer to the ‘a’ variable

  • Step 5 − In the function, increment the value a pointed by the pointer with * operator

  • Step 6 − Print the value inside the function using Println function

  • Step 7 − Finally, print the value of a after function is called as did in last steps

Example

The following example will show how to create Golang program to show scope of variables using pointer approach

package main

import "fmt"

func main() {
   a := 1 
   fmt.Println("Value of a before the function is called:", a)
   increment(&a)  
   fmt.Println("Value of a after the function is called:", a)
}

func increment(ptr *int) {
   *ptr++ 
   fmt.Println("Value of a inside function:", *ptr)  
}

Output

Value of a before the function is called: 1
Value of x inside function: 2
Value of a after the function is called: 2

Using Global Variable Outside the Main Function

In this example, we will write a Golang program to show scope of variables using global variable outside the main function. An external function will be created in which the variables will be manipulated.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and Output

  • Step 2 − Create a global variable and write a string to it

  • Step 3 − Create a main function in which further create a local variable and assign a string to it

  • Step 4 − Print the local variable on the console using Println function

  • Step 5 − In this step, access the global variable in the main function and print the string

  • Step 6 − Call the function callingFunction() from the main

  • Step 7 − In this function, create another local variable using the same variable and print it on the console

  • Step 8 − Again, access the global variable from another function and print its value

  • Step 9 − Then, change the global variable value and print the updated value on the console

  • Step 10 − The print statement is executed using Println function from the fmt package where ln means new line

Example

The following example, illustrates Golang program to show scope of variables using global variable outside the main function

package main

import "fmt"

var global_variable = "I am a global variable"

func main() {
   var local_variable = "I am a local variable"
   fmt.Println(local_variable) 	
   fmt.Println(global_variable)	
   callingFunction()
}

func callingFunction() {	
   var local_variable = "I am another local variable"
   fmt.Println(local_variable) 	
   fmt.Println(global_variable) 	
   global_variable = "I am a new global variable value"	
   fmt.Println(global_variable)
}

Output

I am a local variable
I am a global variable
I am another local variable
I am a global variable
I am a new global variable value

Conclusion

We compiled and executed the program of showing scope of variables using three examples. In the first example, we created a string variable and used function to show local and global variable differently. In the second example, pointer approach is used i.e. the pointer is passed in the function as an argument pointing to the variable which is to be incremented and in the third example we will use global variable outside the main and access it inside the external function.

Updated on: 04-Apr-2023

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements