if语句内的变量作用域

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

Variable scope inside if statements

问题

我刚刚开始学习Go语言,遇到了变量作用域的问题。

我对于在if语句内部创建变量后无法在之后使用感到困惑。

这是我的代码。问题在于new1无法在if语句之前创建,因为它的大小取决于if语句的结果,而在if语句内部创建后,我无法在if语句结束后使用它。

if len(array1)>len(array2) {
    new1 := make([]string,0,len(array1))
    mc := Array2Map_string(array1)
    for _,tok :=range array2 {
        _, ok := mc[tok]
        if ok {
            new1[len(new1)]=tok
            }
        }
    } else {
    new1 := make([]string,0,len(array2))
    mc := Array2Map_string(array2)
    for _,tok :=range array1 {
        _, ok := mc[tok]
        if ok {
            new1[len(new1)]=tok
            }
        }
    }
new2 := make([]string,0,len(new1))
copy(new2, new1)

我能想到的唯一办法是像这样做:

var pointlessvariable uint
if len(array1)>len(array2) {
pointlessvariable=len(array1)
} else {
pointlessvariable=len(array2)
}
var new1 = make([]string,0,pointlessvariable)
if len(array1)>len(array2) {
...

说实话如果这真的是解决办法那我想我不想再使用Go语言了

那么有什么最好的解决方法呢

<details>
<summary>英文:</summary>

I&#39;ve just started learning Go today and have got stuck on variable scopes.

I&#39;ve ultimately confused about how to get around the fact that I can&#39;t create a variable inside an if statement and the use it afterward.

This is my code. The problem is that new1 can&#39;t be created before the if statement because its size is dependent upon the result of the if statement, and by creating it inside the if statement I can&#39;t use it after the if statement ends.

    if len(array1)&gt;len(array2) {
        new1 := make([]string,0,len(array1))
    	mc := Array2Map_string(array1)
    	for _,tok :=range array2 {
    		_, ok := mc[tok]
    		if ok {
    			new1[len(new1)]=tok
    			}
    		}
    	} else {
        new1 := make([]string,0,len(array2))
    	mc := Array2Map_string(array2)
    	for _,tok :=range array1 {
    		_, ok := mc[tok]
    		if ok {
    			new1[len(new1)]=tok
    			}
    		}
    	}
    new2 := make([]string,0,len(new1))
    copy(new2, new1)

The only thing I can think of is to do something like

    var pointlessvariable uint
    if len(array1)&gt;len(array2) {
    pointlessvariable=len(array1)
    } else {
    pointlessvariable=len(array2)
    }
    var new1 = make([]string,0,pointlessvariable)
    if len(array1)&gt;len(array2) {
    ...

To be quite honest if that is truly the solution then I don&#39;t think I want to use Golang after all.

So what is the best way of solving this?

</details>


# 答案1
**得分**: 19

你可以在`if`块之前声明`new1`并在内部使用`make`

```go
var new1 []string

if len(array1) > len(array2) {
    new1 = make([]string, 0, len(array1))
    // 指令...
} else {
    new1 = make([]string, 0, len(array2))
    // 其他指令...
}

new2 := make([]string, 0, len(new1))
copy(new2, new1)
英文:

You can declare new1 before the if block and use make inside:

var new1 []string
if len(array1)&gt;len(array2) {
new1 = make([]string, 0, len(array1))
// instructions ...
} else {
new1 = make([]string, 0, len(array2))
// other instructions ...
}
new2 := make([]string, 0, len(new1))
copy(new2, new1)

答案2

得分: 5

与变量作用域的问题略有关联的是,新的Go语言程序员可能会发现有趣的是,你也可以在代码的任何地方使用一对花括号 { } 来强制变量的作用域。你不需要使用关键字来实现这一点。

示例:

// 在作用域之外
var color = "blue"
{
// 在一个新的作用域内
var color = "red"
fmt.Println(color) // 输出 red
}
// 再次在作用域之外
fmt.Println(color) // 输出 blue again
英文:

Slightly related to the question of variable scope, new gophers (go programmers) might find it interesting that you can also arbitrarily force variable scope with a pair of curly brackets { } anywhere in the code. You don't need a keyword for this.

Example:

// Outside of scope
var color = &quot;blue&quot;
{
// Inside a new scope
var color = &quot;red&quot;
fmt.Println(color) // Prints red
}
// Outside of scope again
fmt.Println(color) // Prints blue again

huangapple
  • 本文由 发表于 2014年6月29日 18:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/24475153.html
匿名

发表评论

匿名网友

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

确定