DEV Community

Discussion on: Why I’m So Frustrated With Go

Collapse
 
blixt profile image
Blixt • Edited

Why not make the map an index into a slice of Animal if you're already going for immutability? Simpler/faster to iterate.

type AnimalMap struct {
    index map[string]int
    items []Animal
}

(The key lookup would become am.items[am.index["value"]])

If you were going for mutability, it'd be simpler to make the index map[string]*Animal (which might not be a bad idea even with immutable access – of course the index matters more/less depending on how much you'd be inserting/deleting.).