douchaqi3369 2016-06-07 20:41
浏览 11
已采纳

赋值且未在if语句中使用的值

I wrote an example of the problem I'm seeing in go playground: https://play.golang.org/p/rPCqAC56Ff

It's pretty self evident, but I'm declaring a variable outside of an if statement, setting variable in if and then using outside of if.

The question is simple, why doesn't this work?

package main

import (
    "fmt"
    "os"
)

func main() {
    var foo string
    if true {
        foo = "foo"
    } else {
        foo, found := os.LookupEnv("GOPATH")
        if !found {
            fmt.Printf("who cares.
")
        }
    }
    println(foo)
}
  • 写回答

2条回答 默认 最新

  • douzuizhuo0587 2016-06-07 22:05
    关注

    From Go documentation:

    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.

    := in the else block redeclares foo, so that foo now refers to a whole new variable with the same name, a variable which is never used. Even if we somehow reached the else block, the println(foo) at last line wouldn't print our $GOPATH as that's stored to the other foo variable (or was until the whole variable went out of scope)

    Correct code would be:

    func main() {
        var foo string
        if true {
            foo = "foo"
        } else {
            var found bool
            foo, found = os.LookupEnv("GOPATH")
            if !found {
                fmt.Printf("who cares.
    ")
            }
        }
        println(foo)
    }
    

    It's easy to get confused since this code works fine even though foo is redeclared before it used:

    func main() {
        var foo string
        foo = "foo"
        foo, found := os.LookupEnv("GOPATH")
        if !found {
            fmt.Printf("who cares.
    ")
        }
        println(foo)
    }
    

    What's happening here is that there's also another, different kind of legal redeclaration in Go:

    Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

    So in this other kind of redeclaration, no new foo variable is actually created and it works just like assignment to foo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?