Answer the question
In order to leave comments, you need to log in
How to group by key and add values in Go?
Hello, I just started looking towards Go, so the most stupid questions arise.
Let's eat
map[string]int {string1:10, string2:20, string1:30, string4:5}
{string1:40, string2:20, string4:5}
Answer the question
In order to leave comments, you need to log in
It's impossible. This simply cannot exist, the entry would overwrite the old one when added to the map, all keys in it are unique, two cannot be the same.
You can make your own type, which will have some kind of add function, which, when such a key is found, will not be overwritten, but summarized. But then you still need to use one of the synchronization methods.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question