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

huangapple go评论125阅读模式
英文:

Redeclare a variable from a different block in a short variable declaration

问题

我如何在不同的代码块中重新声明一个变量,使用短变量声明?

func f() (err os.Error) {
    proc, err := os.StartProcess(blah blah blah)
    // 新的 err 遮盖了返回值?
}

关于这个问题有一个长线程和一个问题,但我现在对如何解决这个问题很感兴趣。

英文:

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.

答案1

得分: 1

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.

答案2

得分: 0

没有办法绕过:=声明的作用域规则。只需明确使用var=

英文:

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

huangapple
  • 本文由 发表于 2011年2月17日 21:54:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/5029819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定