#Map with multiple values
22 messages · Page 1 of 1 (latest)
no, each key can only have one value
but you can use slice/array or struct as the value's type
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?
an array should be fine then for my situation I think
what's the difference between an array and a slice?
you can change the length of the slice
slice actually handle an array's pointer underlying
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
if value1 and value2 have different type, you cannot use an array because it can only handle same type, you have to use struct
is it possible to have a struct that holds multiple values under a key and the values are of different types? ex. int and string?
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
is there an example on this I can find?
type Value struct {
a int
b string
}
type Data struct {
Key1 Value
}
hmmm