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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度