Home > Backend Development > Golang > What are the variable scopes in Go language?

What are the variable scopes in Go language?

WBOY
Release: 2023-06-10 08:03:06
Original
1231 people have browsed it

Variable scope is a very important concept in programming, which determines the visible range and life cycle of variables in the program. In the Go language, variable scope also has its own special rules and restrictions. This article will introduce variable scope in Go language and its principles and applications.

1. Global scope

Global scope refers to the scope in which variables can be accessed and used throughout the program. It is defined at the package level and can be used by variables in the entire package. Used by code in functions, methods, and other files. In the Go language, all variables defined outside a function have global scope and can be accessed throughout the execution of the program.

For example, in the following code, the variable num has a global scope and can be accessed and used in the main function:

package main

import "fmt"

var num int = 10

func main() {
    fmt.Println(num) // 输出 10
}
Copy after login

It should be noted that if the variable num is defined inside a function, If the global variable has the same name, the variables inside the function will overwrite the global variable, but will not affect other functions' access to the global variable.

2. Local scope

Local scope refers to a scope where a variable can only be accessed and used within the code block (usually a function or statement block) in which it is defined. In the Go language, all variables defined inside a function or statement block have local scope and can only be accessed and used inside the function or statement block.

For example, in the following code, the variables x and y have local scope and can only be accessed and used inside the if statement block:

package main

import "fmt"

func main() {
    if x := 10; x > 5 {
        y := 20
        fmt.Println(x, y) // 输出 10 20
    }
    fmt.Println(x, y) // 报错:undefined: y
}
Copy after login

It should be noted that in the Go language Variable definitions can also be placed in statement blocks (such as if, for, switch), and the scope is limited to the inside of the statement block. This method is also called a short declaration of variables.

3. Function parameter scope

The function parameter scope refers to the scope where the parameters of the function are located. In the Go language, function parameters also have local scope and can only be accessed and used within the function.

For example, in the following code, the variable x has function parameter scope and can only be accessed and used inside the function add:

package main

import "fmt"

func add(x, y int) int {
    return x + y + z // 报错:undefined: z
}

func main() {
    z := 10
    fmt.Println(add(1, 2)) // 输出 13
}
Copy after login

It should be noted that the parameters of the function can be used for Passing a variable to a function essentially defines a variable with function parameter scope inside the function.

4. Block scope

Block scope refers to a code block enclosed by curly braces. The variables defined in it can only be accessed in this code block and its sub-code blocks. and use, beyond this range it is inaccessible.

In the Go language, if a variable is defined in a code block, the scope of the variable will be limited to the code block and its sub-code blocks.

For example, in the following code, the variables x, y, and z all have block scope and can only be accessed and used in the corresponding code block and its sub-code blocks:

package main

import "fmt"

func main() {
    x := 1
    if x > 0 {
        y := 2
        fmt.Println(x, y) // 输出 1 2
        if y > 1 {
            z := 3
            fmt.Println(x, y, z) // 输出 1 2 3
        }
        fmt.Println(x, y, z) // 报错:undefined: z
    }
    fmt.Println(x, y) // 报错:undefined: y
}
Copy after login

Required Note that variables defined in a for loop also have block scope, but each loop will create a new variable and will not be affected by previous variables.

Summary

The above introduces the four common variable scopes in the Go language: global scope, local scope, function parameter scope and block scope. Understanding the rules and limitations of variable scope is the basis for writing high-quality code. I hope this article can help readers better understand and use variable scope in the Go language.

The above is the detailed content of What are the variable scopes in Go language?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template