赋值且未在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)
}

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.

You're creating a new variable foo in if block with :=

foo, found := os.LookupEnv("GOPATH")

Check blocks and the scoping rules.

The correct code:

package main

import (
    "fmt"
    "os"
)

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