The scope is the scope of the constant, type, variable, function, or packagerepresented by the declared identifier in the source code.
Go
variables in a language can be declared in three places:
The variables defined in the function are called local variables.
Variables defined outside the function are called global variables
Variables in function definitions are called formal parameters
Next, let’s look at local variables, global variables, and formal parametersin detail. The variables declared in the body of the function are called local variables, their scope is only in the body of the function, and the parameters and return value variables are also local variables. In the following example, the The output of the above example is as follows: Variables declared outside the function are called global variables, which can be used in the entire package or even the external package (after being exported). Global variables can be used in any function, and the following example demonstrates how to use global variables: The output of the above example is as follows: The output of the above example is as follows: Formal arguments are used as local variables of the function. Examples are as follows: The output of the above example is as follows: The default values for different types of local and global variables are: 2.16.1. Local variable #
main()
function uses local variables
a
,
b
,
c
:Example #
package main
import "fmt"
func main() {
/* Declare local variables */
var a, b, c int
/* Initialization parameters */
a = 10
b = 20
c = a + b
fmt.Printf ("result: a = %d, b = %d and c = %d\\n", a, b, c)
}
result: a = 10, b = 20 and c = 30
2.16.2. Global variable #
Example #
package main
import "fmt"
/* Declare global variables */
var g int
func main() {
/* Declare global variables */
var a, b int
/* Initialization parameter */
a = 10
b = 20
g = a + b
fmt.Printf("result: a = %d, b = %d and g = %d\\n", a, b, g)
}
result: a = 10, b = 20 and g = 30
Go
language program, the names of global and local variables can bethe same, but local variables within the function are preferred. Examples are as follows:Example #
package main
import "fmt"
/* Declare global variables */
var g int = 20
func main() {
/* Declare local variables */
var g int = 10
fmt.Printf ("result: g = %d\\n", g)
}
result: g = 10
2.16.3. Formal parameter #
Example #
package main
import "fmt"
/* Declare global variables */
var a int = 20;
func main() {
/* Declaring local variables in the main function */
var a int = 10
var b int = 20
var c int = 0
fmt.Printf("main()In the function a = %d\\n", a);
c = sum( a, b);
fmt.Printf("main()In the function c = %d\\n", c);
}
/* Function Definition - Adding Two Numbers*/
func sum(a, b int) int {
fmt.Printf("sum() In the function a = %d\\n", a);
fmt.Printf("sum() In the function b = %d\\n", b);
return a + b;
}
In the main() function, a=10
In the sum() function, a=10
B=20 in the sum() function
C=30 in the main() function
2.16.4. Initialize local and global variables #