I've looked up Structs as keys in Golang maps
I understand iteration over the maps in golang has no guaranteed order. I've followed the example in golang blog, and tried using a struct
as a map key.
Here's my code
package main
func main() {
req := make(map[mapKey]string)
req[mapKey{1, "r"}] = "robpike"
req[mapKey{2, "gri"}] = "robert griesemer"
req[mapKey{3, "adg"}] = "andrew gerrand"
req[mapKey{4, "rsc"}] = "russ cox"
var keys []int
for k := range req {
keys = append(keys, k.Key)
}
for _, k := range keys {
fmt.Printf("short name : %s , long name : %s\n",req[k], req[k]) // How do I iterate here
}
sort.Ints(keys)
}
type mapKey struct {
Key int
Option string
}
What I want the results to be is
short name : r , long name : rob pike
short name : gri , long name : robert griesemer
short name : adg , long name : andrew gerrand
short name : rsc , long name : russ cox
And I don't know how I can get the struct value and key iterated by separated data structure.