Go 1.2: making slice capacity explicit with three indices
Go 1.2 was announced in December 2013 and introduced the three-index slice expression. A slice's length and its capacity are different properties.
Archive retrospective · Topic year 2013
Historical change and problem
Go 1.2 was announced in December 2013 and introduced the three-index slice expression. A slice's length and its capacity are different properties. Restricting capacity when deriving a smaller view helps a caller express how much of the backing array another function may reuse. This article examines the historical feature rather than claiming results from a benchmark. The key benefit of a capacity limit is reducing the opportunity for another function to extend a slice into shared backing storage unexpectedly. A slice variable should not be mistaken for ownership of the complete array. Code review should describe where the view was created and how long it may be retained.
A concrete design scenario
The three-index form specifies a start, an end, and a maximum capacity boundary. That extra boundary changes the circumstances under which an append can reuse the existing backing storage. Consider a parser passing a narrow region of a larger buffer to another stage. The syntax makes an important assumption more visible, but developers still need to understand which operations share memory. Copying the underlying data and merely restricting a view's capacity are different choices. A deep copy separates storage, while a capacity restriction constrains some forms of append behavior. Their memory costs and aliasing effects differ. Document the intended contract instead of assuming either technique is universally preferable.
Boundaries and failure modes
A common mistake is assuming that copying a slice value automatically copies its elements. The descriptor and the backing array are separate concerns. Changes made through one view can become observable through another. Restricting capacity helps with append behavior but does not by itself establish safe ownership, synchronization, or immutability across concurrent tasks. When several goroutines touch the same elements, slice capacity is not a substitute for a synchronization plan. Two tasks can still race to change shared values. Ownership, race detection, and memory behavior deserve separate evaluation. An API that exposes less capacity does not by itself make the data immutable.
A reproducible acceptance experiment
A useful test records length and capacity separately, then appends to views produced with different capacity limits. Observe which operations allocate fresh storage and which retain the original array. Add boundary and invalid-index cases rather than demonstrating only a successful example. Pin the runtime version: one memory experiment is not proof of a general application speedup. Record initial capacity, append size, and whether allocation occurred in each fixture. Inspect previously created views after the operation rather than checking only the final output. This reveals unintended changes that a simple timing benchmark may miss. Repeat the same procedure with identical starting arrays for reliable comparison.
Maintenance and the lasting lesson
The enduring lesson is that a collection view carries a capacity contract that belongs in API design. Modern teams should document mutability, ownership, and lifetime independently. The release notes establish what changed in 2013; choosing an implementation for a current service still requires checking the supported Go version and the behavior of its libraries. A three-index slice expresses a boundary that would otherwise remain implicit. Its value is in making one aspect of sharing visible to maintainers. If the contract is undocumented, a future refactor can accidentally introduce effects across callers. The third index is therefore an API-design decision, not just a syntactic curiosity.
Comments
No comments yet. Be the first to contribute.
Write a comment
Sample comments and editor reply
Discussion example — not real reader feedback. Names and conversations in this area are fictional. Likes and replies here are not saved and do not count as real engagement.
Example counters: 19,377 reads · 497 likes — not actual analytics
Does restricting slice capacity copy the existing elements?
No. Distinguish the slice descriptor from its backing array. A capacity limit affects some append behavior but does not eliminate sharing of existing elements. Copy the data when separate storage is required.
Features are only part of the story: security and privacy deserve their own review.
I would compare the announcement against independent benchmarks before making a decision.
Reading “Go 1.2: making slice capacity explicit with three indices” today, which limitations of that period still matter?
The topic year provides the historical context. Applying an archive finding today requires checking the source date and current conditions.
Which source would you recommend as a starting point for someone new to yazilim?
Which variables should stay fixed when comparing this result in another setting? A single measurement may not tell the whole story.
Could this link to another archive article on the same subject? Comparing the developments over time would be helpful.
The sources and archive links offer a starting point for further reading. Compare methods and scope as well as the headline claims.
Which questions remain open in “Go 1.2: making slice capacity explicit with three indices”? It would help to separate established findings from expectations.