gpt4 book ai didi

go - 我们在哪里可以在 Go 中使用变量作用域和阴影?

转载 作者:IT王子 更新时间:2023-10-29 01:52:41 25 4
gpt4 key购买 nike

我找到的一些相关帖子:

变量作用域和阴影也有很多用例。
任何代码示例或答案将不胜感激。

最佳答案

变量作用域和阴影:

Go is lexically scoped using blocks:

  1. The scope of a predeclared identifier is the universe block.
  2. The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside any function) is the package block.
  3. The scope of the package name of an imported package is the file block of the file containing the import declaration.
  4. The scope of an identifier denoting a method receiver, function parameter, or result variable is the function body.
  5. The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.
  6. The scope of a type identifier declared inside a function begins at the identifier in the TypeSpec and ends at the end of the innermost containing block.
    An identifier declared in a block may be redeclared in an inner block.
    While the identifier of the inner declaration is in scope, it denotes the entity declared by the inner declaration.

The package clause is not a declaration; the package name does not appear in any scope. Its purpose is to identify the files belonging to the same package and to specify the default package name for import declarations.

优点:

  • 由于无法从外部范围访问数据,因此可以保持数据完整性

Go 中变量阴影的不同形式:

  1. Golang 限制变量范围的方法(在语句中使用简写赋值):

    package main
    import "fmt"
    func main() {
    i := 10 //scope: main
    j := 4
    for i := 'a'; i < 'b'; i++ {
    // i shadowed inside this block
    fmt.Println(i, j) //97 4
    }
    fmt.Println(i, j) //10 4

    if i := "test"; len(i) == j {
    // i shadowed inside this block
    fmt.Println(i, j) // i= test , j= 4
    } else {
    // i shadowed inside this block
    fmt.Println(i, j) //test 40
    }
    fmt.Println(i, j) //10 4
    }
  2. 当“我们需要更多字母表”时,这是限制变量范围的好方法。
    当您需要更多局部变量或范围时,这也很有效:

    使用 {} 对:
    优点:不需要额外的语句,如 if、for、...

    package main
    import "fmt"
    func main() {
    i := 1
    j := 2
    //new scope :
    {
    i := "hi" //new local var
    j++
    fmt.Println(i, j) //hi 3
    }
    fmt.Println(i, j) //1 3
    }
  3. 另一种限制变量范围的方法是使用函数调用:
    优点:范围限制,输入值类型参数像局部变量一样可用,
    缺点:调用/返回时间,和堆栈使用:如果没有被编译器优化

    package main
    import "fmt"
    func fun(i int, j *int) {
    i++ //+nice: use as local var without side effect
    *j++ //+nice: intentionally use as global var
    fmt.Println(i, *j) //11 21
    }
    func main() {
    i := 10 //scope: main
    j := 20
    fun(i, &j)
    fmt.Println(i, j) //10 21
    }
  4. 另一种方法是隐藏全局变量:

    package main
    import "fmt"
    var i int = 1 //global
    func main() {
    j := 2
    fmt.Println(i, j) //1 2
    i := 10 //Shadowing global var
    fmt.Println(i, j) //10 2
    fun(i, j) //10 2
    }
    func fun(i, j int) {
    //i := 100 //error: no new variables on left side of :=
    //var i int = 100 //error: i redeclared in this block
    fmt.Println(i, j) //10 2
    }

参见:Variable shadowingScope .
并且:Declarations and scope :

关于go - 我们在哪里可以在 Go 中使用变量作用域和阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com