W
W
wanomgn2018-10-25 09:25:12
go
wanomgn, 2018-10-25 09:25:12

How to idiomatically increment a value in a map?

package main
//Создаем структуру
type Item struct {
  Data  string
  Count int
}

func main() {
  //сделали мапу
  c := make(map[int]Item)
  //заполнили мапу двумя значениями
  c[10] = Item{"asdf", 1}
  c[20] = Item{"fghj", 1}
}

How to CORRECTLY and beautifully increase Count by 1?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2018-10-25
@wanomgn

Use pointers to structures.
c[10].Count++ will work in this case

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question