在简短的变量声明中重新声明来自不同块的变量

发布于 2024-10-17 20:52:01 字数 412 浏览 4 评论 0原文

如何在简短的变量声明中从不同的块中重新声明变量?

func f() (err os.Error) {
    proc, err := os.StartProcess(blah blah blah)
    // the new err masks the return value?
}

有一个关于此问题的长线程,还有一个问题,但我暂时对如何解决此问题感兴趣。

How can I redeclare a variable from a different block in a short variable declaration?

func f() (err os.Error) {
    proc, err := os.StartProcess(blah blah blah)
    // the new err masks the return value?
}

There's a long thread about this, and an issue, but I'm interested in how to work around this for the time being.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-10-24 20:52:01

短变量声明的 Go 规范很明确:

简短的变量声明可以
重新声明变量,前提是它们是
最初在同一块中声明
具有相同类型,且至少有一个
非空白变量是新的。

因此,在简短的变量声明中,您不能重新声明最初在不同块中声明的变量。

下面的示例说明了如何通过在内部块中声明局部变量 (e) 并将其 (e) 分配给变量 (err2) 在外部块中声明。

package main

import (
    "fmt"
    "os"
)

func f() (err1 os.Error, err2 os.Error) {
    fi, err1 := os.Stat("== err1 os.Error ==")
    _ = fi
    {
        fi, e := os.Stat("== e os.Error ==")
        _ = fi
        err2 = e
    }
    return
}

func main() {
    err1, err2 := f()
    fmt.Println("f() err1:", err1)
    fmt.Println("f() err2:", err2)
}

输出:

f() err1: stat == err1 os.Error ==: no such file or directory
f() err2: stat == e os.Error ==: no such file or directory

这是前面的示例,重写为使用显式常规 变量声明 并命名为 函数参数而不是隐式短变量声明。变量声明始终可以显式编写为常规变量声明或命名函数参数;隐式短变量声明只是常规变量声明的简写。

package main

import (
    "fmt"
    "os"
)

func f() (err1 os.Error, err2 os.Error) {
    var fi *os.FileInfo
    fi, err1 = os.Stat("== err1 os.Error ==")
    _ = fi
    {
        var fi *os.FileInfo
        fi, err2 = os.Stat("== err2 os.Error ==")
        _ = fi
    }
    return
}

func main() {
    err1, err2 := f()
    fmt.Println("f() err1:", err1)
    fmt.Println("f() err2:", err2)
}

输出:

f() err1: stat == err1 os.Error ==: no such file or directory
f() err2: stat == err2 os.Error ==: no such file or directory

在您的示例中,err 的短变量声明重新声明了 err 的返回参数声明;他们在同一个街区。因此,新的err不会屏蔽返回参数err

The Go specification for short variable declarations is clear:

a short variable declaration may
redeclare variables provided they were
originally declared in the same block
with the same type, and at least one
of the non-blank variables is new.

Therefore, in a short variable declaration, you can't redeclare variables originally declared in a different block.

Here's an example of how to get around this restriction by declaring a local variable (e) in the inner block and assigning it (e) to a variable (err2) declared in an outer block.

package main

import (
    "fmt"
    "os"
)

func f() (err1 os.Error, err2 os.Error) {
    fi, err1 := os.Stat("== err1 os.Error ==")
    _ = fi
    {
        fi, e := os.Stat("== e os.Error ==")
        _ = fi
        err2 = e
    }
    return
}

func main() {
    err1, err2 := f()
    fmt.Println("f() err1:", err1)
    fmt.Println("f() err2:", err2)
}

Output:

f() err1: stat == err1 os.Error ==: no such file or directory
f() err2: stat == e os.Error ==: no such file or directory

Here's the previous example rewritten to use explicit regular variable declarations and named function parameters rather than implicit short variable declarations. Variable declarations can always be written explicitly as regular variable declarations or named function parameters; implicit short variable declarations are merely a shorthand for regular variable declarations.

package main

import (
    "fmt"
    "os"
)

func f() (err1 os.Error, err2 os.Error) {
    var fi *os.FileInfo
    fi, err1 = os.Stat("== err1 os.Error ==")
    _ = fi
    {
        var fi *os.FileInfo
        fi, err2 = os.Stat("== err2 os.Error ==")
        _ = fi
    }
    return
}

func main() {
    err1, err2 := f()
    fmt.Println("f() err1:", err1)
    fmt.Println("f() err2:", err2)
}

Output:

f() err1: stat == err1 os.Error ==: no such file or directory
f() err2: stat == err2 os.Error ==: no such file or directory

In your example, the short variable declaration of err redeclares the return parameter declaration of err; they are in the same block. Therefore, the new err does not mask the return parameter err.

雨后彩虹 2024-10-24 20:52:01

无法绕过 := 声明的范围规则。只需明确使用 var= 即可。

There is no way to bypass the scope rules of a := declaration. Just be explicit using var and =.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文