golang定义dict像python一样,并在dict中将值附加到列表中

I am new to go and trying to implement python like nested structure as below, I am not able to define the empty dict/map in golang that can have list of specific struct/classobj and while iterating through the data I am not able to append items in map/dict ... I will really appreciate any help on this ... Thanks

items = [
    ("item1", someObj1),
    ("item2", someObj2),
    ("item3", someObj3),
    ("item3", someObj5),
    ("item1", someObj4),
]

rectors = {}
for item, obj in items:
    try:
        rectors[item].append(obj)
    except KeyError:
        rectors[item] = [obj]

print rectors
# OUTPUT: {'item2': [someObj2], 'item3': [someObj3, someObj5], 'item1': [someObj1, someObj4]}

Its not quite as clean .. but this does roughly what you want and should get you started on the right path:

type someObj struct {
}

func main() {
    items := map[string][]someObj{
        "item1": []someObj{someObj{}},
        "item2": []someObj{someObj{}},
        "item3": []someObj{someObj{}},
    }

    items["item1"] = append(items["item1"], someObj{})

    rectors := make(map[string][]someObj)    

    for key, val := range items {
        if obj, exists := rectors[key]; exists {
            rectors[key] = append(obj, val...)
        } else {
            rectors[key] = val
        }
    }

    fmt.Printf("%v", rectors)
}

Output:

map[item3:[{}] item1:[{} {}] item2:[{}]]

The main difference being .. you can't initialize the map and alter an item with a key that already exists (as you appear to be doing in your example with item1 being appended during initialization). So that becomes an extra step after the initialization of the map. You could always just do:

"item1": []someObj{someObj{}, someObj{}},

.. but that didn't seem to be the same as what you were doing.

See it on the Go playground