Go: append directly to slice found in a map

Viewed 12650

I wanted to create a map of slices where values are appended to the corresponding slice. However, when trying to append directly to the slice returned by accessing it (see comment below), it would not be stored, so I had to go with the long form access (line below the comment).

Why is it so? I expected the access to the map to return some sort of pointer, so in my mind mappedAminoAcid == aminoAcidsToCodons[aminoAcid]; clearly, I'm wrong.

Thanks!

aminoAcidsToCodons := map[rune][]string{}
for codon, aminoAcid := range utils.CodonsToAminoAcid {
    mappedAminoAcid, ok := aminoAcidsToCodons[aminoAcid]

    if ok {
        // NOT WORKING: mappedAminoAcid = append(mappedAminoAcid, codon)
        aminoAcidsToCodons[aminoAcid] = append(mappedAminoAcid, codon)
    } else {
        aminoAcidsToCodons[aminoAcid] = []string{codon}
    }
}
1 Answers

append returns a new slice if the underlying array has to grow to accomodate the new element. So yes, you have to put the new slice back into the map. This is no different from how strings work, for instance:

var x map[string]string
x["a"] = "foo"

y := x["a"]
y = "bar"

// x["a"] is still "foo"

Since a nil slice is a perfectly fine first argument for append, you can simplify your code to:

aminoAcidsToCodons := map[rune][]string{}
for codon, aminoAcid := range utils.CodonsToAminoAcid {
    aminoAcidsToCodons[aminoAcid] = append(aminoAcidsToCodons[aminoAcid], codon)
}
Related