在地图中设置和逐步递增一个值的实用方法是什么?

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

practical way to set and sequentially increment a value inside a map?

问题

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. var store = map[string]int{}
  6. func breadArrived(num int) {
  7. if breadTotal, ok := store["bread"]; ok {
  8. breadTotal += num
  9. } else {
  10. store["bread"] = num
  11. }
  12. fmt.Printf("%v\n", store)
  13. }
  14. func main() {
  15. breadArrived(1)
  16. breadArrived(2)
  17. breadArrived(3)
  18. }

上面的代码忽略了 += 运算符,所以 store["bread"] 总是等于 1。我猜我在这里漏掉了类似于 "按引用传递" 的东西。此外,有没有更方便的方法来做到这一点?

英文:

<!-- language: go -->

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. var store = map[string]int{}
  6. func breadArrived(num int) {
  7. if breadTotal, ok := store[&quot;bread&quot;]; ok {
  8. breadTotal += num
  9. } else {
  10. store[&quot;bread&quot;] = num
  11. }
  12. fmt.Printf(&quot;%v\n&quot;, store)
  13. }
  14. func main() {
  15. breadArrived(1)
  16. breadArrived(2)
  17. breadArrived(3)
  18. }

Code above ignores the += operator, so store[&quot;bread&quot;] always equals to 1. I assume I'm missing something like "passing by reference" here. Also, is there any more convenient way of doing this?

答案1

得分: 19

你只是增加了breadTotal这个局部变量的值,而没有增加store映射中的值。应该是这样的:

  1. store["bread"] = breadTotal + num

另外,你也可以简单地这样做:

  1. store["bread"] += num

另外,由于对映射进行索引会返回映射中尚未存在的键的零值int的零值是0,表示没有面包),所以if语句是完全不必要的。你可以简单地这样做:

  1. func breadArrived(num int) {
  2. store["bread"] += num
  3. fmt.Printf("%v\n", store)
  4. }

输出结果(在Go Playground上尝试):

  1. map[bread:1]
  2. map[bread:3]
  3. map[bread:6]
英文:

You're only incrementing the breadTotal local variable, and not the value in the store map. It should be:

  1. store[&quot;bread&quot;] = breadTotal + num

Also you can simply do:

  1. store[&quot;bread&quot;] += num

Also since indexing a map returns the zero value of the value type for keys which are not yet in the map (zero value for int is 0 – properly telling no bread yet), that if is completely unnecessary. You can simply do:

  1. func breadArrived(num int) {
  2. store[&quot;bread&quot;] += num
  3. fmt.Printf(&quot;%v\n&quot;, store)
  4. }

Output (try it on the Go Playground):

  1. map[bread:1]
  2. map[bread:3]
  3. map[bread:6]

答案2

得分: 3

breadTotal变量只是map整数值的一个副本,你需要将其引用为store["bread"] += num来增加map整数值。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. var store = map[string]int{}
  6. func breadArrived(num int) {
  7. store["bread"] += num
  8. fmt.Printf("%v\n", store)
  9. }
  10. func main() {
  11. breadArrived(1)
  12. breadArrived(2)
  13. breadArrived(3)
  14. }

顺便说一下,第一次增加map整数的操作有效是因为Go语言将所有变量初始化为默认值,在整数的情况下,默认值是0

英文:

The breadTotal variable is only a copy of the map integer value, you need to reference it as store[&quot;bread&quot;] += num to increment the map integer value.

https://play.golang.org/p/LQzrbSZudH

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. var store = map[string]int{}
  6. func breadArrived(num int) {
  7. store[&quot;bread&quot;] += num
  8. fmt.Printf(&quot;%v\n&quot;, store)
  9. }
  10. func main() {
  11. breadArrived(1)
  12. breadArrived(2)
  13. breadArrived(3)
  14. }

By the way, the first incrementing of the map integer works because go initializes everything to a default value, in the case of integers the default value is 0.

答案3

得分: 0

简单的增量操作,类似于C/C++风格:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. var store = map[string]int{}
  6. func main() {
  7. store["some_key"] = 0
  8. if _, found := store["some_key"]; found {
  9. fmt.Println(store["some_key"])
  10. store["some_key"]++
  11. fmt.Println(store["some_key"])
  12. }
  13. }

点击此处查看代码。

英文:

Simple increment like C\C++ style:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. var store = map[string]int{}
  6. func main() {
  7. store[&quot;some_key&quot;] = 0
  8. if _, found:= store[&quot;some_key&quot;]; found{
  9. fmt.Println(store[&quot;some_key&quot;])
  10. store[&quot;some_key&quot;]++
  11. fmt.Println(store[&quot;some_key&quot;])
  12. }
  13. }

https://play.golang.org/p/nyM7GFI1-GW

huangapple
  • 本文由 发表于 2017年1月25日 20:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/41851566.html
匿名

发表评论

匿名网友

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

确定