C
C
Chronic 862016-08-29 11:08:53
go
Chronic 86, 2016-08-29 11:08:53

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}

It is required to group by key and add the values, i.e. get:
{string1:40, string2:20, string4:5}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2016-08-29
@chronic86

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 question

Ask a Question

731 491 924 answers to any question