11 Scope of Go

Overview

        In the previous section, we introduced Go’s mapping, including: declaration mapping, initialization mapping, operation mapping, etc. In this section, we will introduce scoping in Go. In the Go language, scope refers to the visibility range of a variable, which defines the life cycle and accessibility of the variable in the program. The scopes in the Go language can be divided into the following types: local scope, global scope, and namespace scope, which are introduced below.

local scope

        Local scope is also called function scope, which means that all variables and constants defined inside a function have local scope. The life cycle of these variables and constants is limited to the inside of the function and is not visible outside the function.

        When you declare a variable or constant inside a function, it is created in the local scope. These variables and constants are valid during the execution of the function or method and can be used within the function or method's code block. Once the function or method ends, these variables and constants will be destroyed and their memory space will be released.

package main

import "fmt"

func main() {
    // 局部变量作用域  
    {  
        var localVar = "Hello CSDN"
        // 输出:Hello CSDN
        fmt.Println(localVar)
    }  
  
    // 局部变量作用域已经结束,到这里时localVar不再可见
    // 编译错误:undefined: localVar
    fmt.Println(localVar)
}

        In the above example code, localVar is a local variable defined in the code block inside the main function, and its scope is limited to the code block inside the function. Within the code block of the main function, we can use localVar and it will not be visible to the outside. Once you leave the main function's code block, trying to access localVar will result in a compilation error because it has exceeded the scope of the local scope.

global scope

        The global scope is also called the package scope, which refers to the scope in which variables can be accessed and used throughout the program. Global variables are declared outside a function and can be used throughout the entire package's code files. In the Go language, all variables defined outside a function have global scope, which means that they can be accessed throughout the execution of the program.

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

package main

import "fmt"

// 全局变量声明  
var globalVar = "Global CSDN"

func main() {
    // 局部变量声明  
    var localVar = "Local CSDN"  

    // 输出:Global CSDN
    fmt.Println(globalVar)
    // 输出:Local CSDN
    fmt.Println(localVar)
  
    // 同名局部变量覆盖全局变量
    func() {
        var globalVar = "Local CSDN 2"
        // 输出:Local CSDN 2
        fmt.Println(globalVar)  
    }()  
  
    // 函数内部访问全局变量
    func(x int) {
        // 输出:Global CSDN
        fmt.Println(globalVar)
        // 输出:66
        fmt.Println(x)
    }(66)  
}

        In the above example code, we declare a global variable globalVar and a local variable localVar inside the main function. We can access the global variable globalVar from within the main function, as well as from within nested functions. When a local variable globalVar with the same name is declared inside a nested function, the global variable globalVar will be overwritten.

        ​​​​​Note: Global variables can be shared and accessed between any functions within the same package. However, global variables are independent between different packages, that is, each package has its own set of global variables.

namespace scope

        Namespace scope refers to the independent scope of each package in the code file. Each package has its own namespace, in which the variables, functions, types, etc. declared are visible in the code files of the package but not in other packages.

        Namespace scope is used to isolate code between different packages to avoid naming conflicts and accidental access. Code under the same namespace can freely access and share variables, functions, types, etc., but code between different namespaces is independent of each other.

        In Go language, if we declare a variable or function in a package, other packages cannot directly access the variable or function unless we access it by importing the package and using the exported name of the variable or function. This namespace isolation mechanism helps maintain code clarity and maintainability.

        In the following example code, we have written two go source code files, one of which is the example1.go file.

// 文件名:example1.go
package example1

var Var1 = "Hello, CSDN"

func Func1() {
    fmt.Println(Var1)
}

        The other one is the example2.go file.

// 文件名:example2.go
package example2
  
import "fmt"
import "example1"
  
func main() {
    // 输出:Hello, CSDN
    fmt.Println(example1.Var1)
    // 输出:Hello, CSDN
    example1.Func1()
}

        In the above example code, the example1 package and example2 package have independent namespaces. In the example1 package, we declare the variable Var1 and the function Func1, which are visible in the package's code files. In the example2 package, we imported the example1 package and tried to access the variables and functions in it. By using the name of the imported package as a prefix, we can access the variables and functions in the example1 package.

Guess you like

Origin blog.csdn.net/hope_wisdom/article/details/134365526