英文:
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 (
"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)
}
Code above ignores the += operator, so store["bread"] 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["bread"] = breadTotal + num
Also you can simply do:
store["bread"] += 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["bread"] += num
fmt.Printf("%v\n", 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["bread"] += num to increment the map integer value.
https://play.golang.org/p/LQzrbSZudH
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)
}
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 (
"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"])
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论