D
D
dev2expert2015-08-17 11:32:04
go
dev2expert, 2015-08-17 11:32:04

How to fill in maps in go?

There is such a type:
map[int]map[int]map[int]int
To store such a data structure:

-- [ id ]
------ [ group_id ]
---------- [ item_id ]
-------------- [ quantity ]
---------- [ item_id ]
-------------- [ quantity ]
---------- [ item_id ]
-------------- [ quantity ]
------ [ group_id ]
---------- [ item_id ]
-------------- [ quantity ]
--[ id ]
------ [ group_id ]
---------- [ item_id ]
-------------- [ quantity ]

How to correctly add group_id to such maps?
Right now I'm doing something like this:
if _, ok := my_map[group_id]; !ok {
    my_map[group_id] = make(map[int]int)
}
my_map[group_id][item_id] = quantity

and then my_map is added to the global map, this code seems too scary to me, maybe there are best practices for working with such data?
Is it possible to implement this only with slices?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SilentFl, 2015-08-17
@dev2expert

Go has OOP, use it. Highlight your Item, Group, etc. at least like this

V
Vitaly Filinkov, 2015-08-17
@vitfil

I am absolutely delighted! The texture is just fabulous!
Subject: Permission!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question