#Map with multiple values

22 messages · Page 1 of 1 (latest)

summer kite
#

Hello,

Does anyone know of a good way of instantiating a map and then adding multiple values to a key in a map? What I'm looking for is something like this:

"Key1": value1, value2
gleaming harbor
#

no, each key can only have one value

#

but you can use slice/array or struct as the value's type

summer kite
#

is there a data structure that allows for this?

#

probably an array then?

gleaming harbor
#

array will be more compactness, the disadvantage of an array is every single key must handle same value amount

#

like for "key1" you assign it value1 and value2, for "key2" you also have to assign it 2 values so maybe value3 and value4. You cannot assign more or less

#

same as struct

#

idk what are you trying to do, can you ask X question?

summer kite
#

an array should be fine then for my situation I think

summer kite
gleaming harbor
#

you can change the length of the slice

#

slice actually handle an array's pointer underlying

summer kite
#

what I'm really looking to do is have an overlying struct's property to either be an array or slice. and then I'm gonna put it through a for loop and when it meets a condition I need to add Key1: value1, value2 to the property where Key1 is a string type, value1 is a float type, and value2 is a string type. These types will never change througout the loop and the length of the array will never change

#

so there's going to be 2 values associated for one key

gleaming harbor
summer kite
gleaming harbor
#

yes, same as the map, you have to define another struct that handle int and string, and use the struct as the field's value type

summer kite
gleaming harbor
#
type Value struct {
  a int
  b string
}
type Data struct {
  Key1 Value
}
summer kite
#

hmmm