Golang slices have dual behavior, acting as either dynamically sized arrays or fat pointers, which can cause confusion and bugs. Dynamic arrays own their data, while fat pointers do not, making some operations like appending invalid depending on the context. The capacity field in slices introduces surprising behaviors like reslicing beyond the length and modifying capacity, contradictory to Go's minimalist philosophy.

4m read timeFrom build-your-own.org
Post cover image
Table of contents
Is a slice a dynamically sized array?Is a slice a fat pointer?A slice cannot be both!Muddling data mutability and ownershipConclusion: Always clarify what a slice is!Merging incompatible concepts was a mistakeMore surprises: Reslice beyond lengthMore surprises: Modify the capacity field
1 Comment

Sort: