dictionary - Hash with key as an array type -


how create key array in go map. example in ruby can implement such:

quarters = {   [1, 2, 3] => 'first quarter',   [4, 5, 6] => 'second quarter',   [7, 8 ,9] => 'third quarter',   [10, 11, 12] => 'fourh quarter', } quarters[[1, 2, 3]]  # => "first quarter" 

how same looked in golang ?

array types (unlike slices) in go comparable, there nothing magical in it: can define other maps: map[keytype]valuetype keytype [3]int , valuetype string.

the comparison operators == , != must defined operands of key type; key type must not function, map, or slice.

m := map[[3]int]string{}  m[[3]int{1, 2, 3}] = "first quarter" m[[3]int{4, 5, 6}] = "second quarter" m[[3]int{7, 8, 9}] = "third quarter" m[[3]int{10, 11, 12}] = "fourth quarter"  fmt.println(m) 

output:

map[[1 2 3]:first quarter [4 5 6]:second quarter      [7 8 9]:third quarter [10 11 12]:fourth quarter] 

try on go playground.

to query element:

fmt.println(m[[3]int{1, 2, 3}]) // prints "first quarter" 

you can create map in 1 step:

m := map[[3]int]string{     [3]int{1, 2, 3}:    "first quarter",     [3]int{4, 5, 6}:    "second quarter",     [3]int{7, 8, 9}:    "third quarter",     [3]int{10, 11, 12}: "fourth quarter", } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -