英文:
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've just started learning Go today and have got stuck on variable scopes.
I've ultimately confused about how to get around the fact that I can't create a variable inside an if statement and the use it afterward.
This is my code. The problem is that new1 can'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't use it after the if statement ends.
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)
The only thing I can think of is to do something like
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) {
...
To be quite honest if that is truly the solution then I don'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)>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 = "blue"
{
// Inside a new scope
var color = "red"
fmt.Println(color) // Prints red
}
// Outside of scope again
fmt.Println(color) // Prints blue again
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论