Go:如何声明一个新变量并覆盖来自更高作用域的变量?

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

Go: Declare both a new variable and overwrite a variable from a higher scope, how?

问题

我无法在任何地方找到答案,所以这是情况:

// err在这里尚未声明
globalVar := "string"
if globalVar == "string" {
globalVar, err := doSomethingWithString()
if err != nil {
// 错误处理
}
}

第二个globalVar声明在使用:==时都会出错:

  • 使用:=时,它会显示globalVar已声明但未使用,因为现在globalVar是内部作用域中的新变量。
  • 使用=时,它会显示未定义的err,因为它尚未声明。

所以基本上,应该有一种方法来定义声明左侧每个变量的=:=之间的区别。

我看到两种可能的解决方案,我认为它们都同样丑陋:

// err在这里尚未声明
globalVar := "string"
if globalVar == "string" {
globalVar2, err := doSomethingWithString()
if err != nil {
// 错误处理
}
globalVar = globalVar2
}

或者

globalVar := "string"
var err error
if globalVar == "string" {
globalVar, err = doSomethingWithString()
if err != nil {
// 错误处理
}
}

我必须使用这些解决方法之一吗?还是有一种正确的方法可以实现我所需的功能?

第二种解决方案似乎最不丑陋,但如果有许多变量只在if作用域中使用,这些变量将不会在作用域之后被删除,并且会持续整个外部作用域。所以我认为第一种解决方案是最好的。

但我想听听其他人是如何解决这种情况的...

英文:

I cannot find the answer anywhere, so this is the situation:

// err has not yet been declared here
globalVar := "string"
if globalVar == "string" {
	globalVar, err := doSomethingWithString()
	if err != nil {
		// error handling
	}
}

That second globalVar declaration gives an error both then := and when '=' is used:

  • With := it says globalVar declared and not used because now globalVar is a new variable in the inner scope.
  • With = it says undefined err because it has not yet been declared.

So basically, there should be a way to define the difference between = and := for each variable on the left side of the declaration.

I see two possible solutions, which are both equally ugly in my opinion:

// err has not yet been declared here
globalVar := "string"
if globalVar == "string" {
	globalVar2, err := doSomethingWithString()
	if err != nil {
		// error handling
	}
	globalVar = globalVar2
}

or

globalVar := "string"
var err error
if globalVar == "string" {
	globalVar, err = doSomethingWithString()
	if err != nil {
		// error handling
	}
}

Do I have to use one of these work-arounds or is there a correct way of achieving what I need?

The second solution seems the least ugly, but if there are many variables you only need in the if-scope, these variables will not be removed after the scope and persist the whole outer scope. So the fist solution seems the best in my opinion.

But I'd like to hear how others resolve this situation...

答案1

得分: 19

只是在这种情况下不要使用:=,并预先声明globalVar和err。

var (
    globalVar string
    err error
)

globalVar = "string"
if globalVar == "string" {
    globalVar, err = doSomethingWithString()
    if err != nil {
        // 错误处理
    }
}

或者,如果你想限制err的范围:

globalVar := "string"
if globalVar == "string" {
    var err error
    globalVar, err = doSomethingWithString()
    if err != nil {
        // 错误处理
    }
}

http://play.golang.org/p/73bF_tHYsC 是一个类似于前面答案的示例。

:=的目的是在你想要在一个语句中声明和赋值时提供方便。如果它妨碍了你,就不要使用它。

英文:

Just don't use := in this case and predeclare both globalVar and err.

var (
    globalVar string
    err error
)

globalVar = "string"
if globalVar == "string" {
    globalVar, err = doSomethingWithString()
    if err != nil {
        // error handling
    }
}

or alternatively if you want to limit the scope of err:

globalVar := "string"
if globalVar == "string" {
    var err error
    globalVar, err = doSomethingWithString()
    if err != nil {
        // error handling
    }
}

http://play.golang.org/p/73bF_tHYsC for an example similar to the previous answer.

:= is meant as a convenience when you want to declare and assign in one statement. If it's getting in the way just don't use it.

huangapple
  • 本文由 发表于 2013年6月14日 21:59:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/17110268.html
匿名

发表评论

匿名网友

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

确定