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

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

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

问题

package main

import (
	"fmt"
)

var store = map[string]int{}

func breadArrived(num int) {
	if breadTotal, ok := store["bread"]; ok {
		breadTotal += num
	} else {
		store["bread"] = num
	}
	fmt.Printf("%v\n", store)
}

func main() {
	breadArrived(1)
	breadArrived(2)
	breadArrived(3)
}

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

英文:

<!-- language: go -->

package main

import (
	&quot;fmt&quot;
)

var store = map[string]int{}

func breadArrived(num int) {
	if breadTotal, ok := store[&quot;bread&quot;]; ok {
		breadTotal += num
	} else {
		store[&quot;bread&quot;] = num
	}
	fmt.Printf(&quot;%v\n&quot;, store)
}

func main() {
	breadArrived(1)
	breadArrived(2)
	breadArrived(3)
}

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映射中的值。应该是这样的:

store["bread"] = breadTotal + num

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

store["bread"] += num

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

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

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

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

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

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

Also you can simply do:

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:

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

Output (try it on the Go Playground):

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

答案2

得分: 3

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

package main

import (
	"fmt"
)

var store = map[string]int{}

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

func main() {
	breadArrived(1)
	breadArrived(2)
	breadArrived(3)
}

顺便说一下,第一次增加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

package main

import (
	&quot;fmt&quot;
)

var store = map[string]int{}

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

func main() {
	breadArrived(1)
	breadArrived(2)
	breadArrived(3)
}

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++风格:

package main

import (
	"fmt"
)

var store = map[string]int{}

func main() {
	store["some_key"] = 0
	if _, found := store["some_key"]; found {
		fmt.Println(store["some_key"])
		store["some_key"]++
		fmt.Println(store["some_key"])
	}
}

点击此处查看代码。

英文:

Simple increment like C\C++ style:

package main

import (
	&quot;fmt&quot;
)

var store = map[string]int{}

func main() {
	store[&quot;some_key&quot;] = 0
	if _, found:= store[&quot;some_key&quot;]; found{
		fmt.Println(store[&quot;some_key&quot;])
		store[&quot;some_key&quot;]++
		fmt.Println(store[&quot;some_key&quot;])
	}
}

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:

确定