"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Go Slices vs. Maps: Why Do Appends Behave Differently?

Go Slices vs. Maps: Why Do Appends Behave Differently?

Posted on 2025-03-24
Browse:904

Go Slices vs. Maps: Why Do Appends Behave Differently?

Slice vs Map: Understanding Parameter Reference Behavior

In Go, slices and maps both represent reference types. When modifying an element in either type, the change reflects in all slices/maps derived from them. This shared modification behavior results from the pointer-based implementation of maps, where modifications made through a pointer apply to the original map.

However, when adding new elements, slices and maps behave differently. In the provided code example, appending new elements to a map (editMap) results in those elements being accessible through the original map. However, appending to a slice (editSlice) has no effect on the original slice.

This discrepancy stems from the underlying implementation differences between slices and maps. Maps are implemented as pointers to hash map structures, while slices are lightweight structs that reference an array. When adding a new element to a map, the pointer remains constant, leading to the visibility of the new element in all derived maps.

Contrastingly, when appending to a slice, the slice header (describing the slice's length, capacity, and array pointer) must be modified. However, since Go passes variables by value, any changes made to the copy of the slice header (in the passed-in parameter) are not reflected in the original slice.

To achieve consistent behavior between slices and maps, one could implement slices as pointers, similar to maps. This would ensure that modifications made through pointers to slices also modify the original slice. However, this approach is uncommon in Go due to the lack of language support for passing and working with slice pointers. Instead, the convention of returning new slices after modification is widely adopted.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3