为什么使用空白标识符进行范围迭代会产生不同的值?

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

Why does range iterating with _ blank identifier produce different values

问题

我正在学习Go,目前感觉非常好。

以下代码输出的和为45

package main
import "fmt"

func main(){
   //声明一个切片
   numSlice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   var sum int = 0

   for num := range numSlice {                                                                                            sum += num
      fmt.Println("num =", num)
   }
   fmt.Println("sum =", sum)   
}

以下代码中,我使用 _ 作为空白标识符来忽略循环中的索引,输出的和为55

//声明一个切片
numSlice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var sum int = 0

for _, num := range numSlice {                                                                                            sum += num
   fmt.Println("num =", num)
}
fmt.Println("sum =", sum)   

这让我有点困惑。根据我的理解,空白标识符用于忽略切片的索引。但它似乎也会改变索引的位置,从而忽略了切片的最后一个元素。

你能解释一下这里发生了什么,并可能解释一下原因吗?我假设这不是一个 bug,而是有意设计的。Go 语言设计得非常好,那么这种行为可能有哪些可能的用例呢?

英文:

I'm learning Go and having a great time so far.

The following code outputs the sum as 45

<!-- language-all: go -->

package main
import &quot;fmt&quot;

func main(){
   //declare a slice
   numSlice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
   var sum int = 0

   for num := range numSlice {                                                                                           
      sum += num
      fmt.Println(&quot;num =&quot;, num)
   }
   fmt.Println(&quot;sum =&quot;, sum)   
}

The following code, where I use _ the blank identifier to ignore the index in the for declaration outputs the sum as 55

//declare a slice
numSlice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var sum int = 0

for _,num := range numSlice {                                                                                           
   sum += num
   fmt.Println(&quot;num =&quot;, num)
}
fmt.Println(&quot;sum =&quot;, sum)   

This has got me slightly stumped. From my understanding the blank identifier is used to ignore the slice index . But it also seems to be shifting the index and thereby ignoring the last element in the slice.

Can you please explain what's happening here and possibly why. I'm assuming this is not a bug and is by design. Go is so well designed so what would the possible use cases be for this kind of behaviour?

答案1

得分: 8

单参数范围使用索引而不是值。因为你的索引也是从0到9递增的,使用单参数的范围将把索引从0加到9,并给你一个结果为45的和。

package main
import "fmt"

func main(){
   //声明一个切片
   numSlice := []int{0, 0, 0, 0}
   var sum int = 0

   for num := range numSlice {                                                                                            sum += num
      fmt.Println("num =", num)
   }
   fmt.Println("sum =", sum)   
}

输出

num = 0
num = 1
num = 2
num = 3
sum = 6
英文:

Single parameter range uses indexes, not values. Because your indexes are also going up from 0 to 9 using range with a single param will add the indexes up from 0 to 9 and give you 45

package main
import &quot;fmt&quot;

func main(){
   //declare a slice
   numSlice := []int{0, 0, 0, 0}
   var sum int = 0

   for num := range numSlice {                                                                                           
      sum += num
      fmt.Println(&quot;num =&quot;, num)
   }
   fmt.Println(&quot;sum =&quot;, sum)   
}

Output

num = 0
num = 1
num = 2
num = 3
sum = 6

huangapple
  • 本文由 发表于 2014年10月23日 19:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/26526896.html
匿名

发表评论

匿名网友

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

确定