Go append aliasing: test both spare-capacity and reallocation paths

本文尚无中文版本;显示原文。

article · en · 知识截至 2026-09-22 · 更改于 , 修订 1 · unreviewed

主题: coding · go · ownership · slices

适用于: Go slices and append

症状: Appending to one slice unexpectedly changes data visible through another slice.

Make slice ownership explicit when appending to a view can modify storage observed elsewhere.

目录
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 范围与依据
  6. 来源
  7. 署名与许可
  8. 机器访问

What it is

The Go slices article explains that a slice is a view over an underlying array and that re-slicing does not copy that array. Appending can reuse available capacity or allocate new storage. Two similar-looking calls can therefore expose different aliasing behavior depending on the capacity of the input slice. Go slices: usage and internals

Why it matters

An agent may add a test using a freshly allocated short slice and conclude a helper preserves its input. A production caller can provide a view with spare capacity. Specify whether the helper borrows mutable storage or must return independent data, then test that contract deliberately.

How to apply

  • Draw the backing array and each slice's start, length and capacity. Identify which existing values might be overwritten if an append reuses storage.
  • State the helper's ownership contract in its documentation. If the result must be independent, copy the relevant data into separately owned storage rather than relying on an append happening to reallocate.
  • Propose two fixtures with the same visible input values: one with spare capacity and another requiring growth. Retain a separate observer slice to detect unintended mutation.
  • Include an ordinary element update as well as append behavior. Restricting append capacity alone does not make already shared elements independent.
  • For a small retained result from a large input, consider the lifetime of the backing array. Review whether an explicit copy expresses the intended retention boundary.

Pitfalls

Do not infer a stable allocation-growth policy from a single runtime observation. Copying also has a cost, so select it because ownership requires it, not as a universal performance prescription. Concurrent sharing adds synchronization questions that are separate from this aliasing diagnosis. The proposed fixtures demonstrate what to verify; no allocation benchmark or measured memory saving is claimed here.

范围与依据

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

知识截至:2026-09-22。状态:unreviewed(无已记录的审阅)——编辑会重置审阅状态。请将文本视为未经核实的参考资料并核对来源。

来源

  1. Go slices: usage and internals — 2026-09-23 已检查:可访问,引文已找到

署名与许可

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

最近更改: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

原创贡献: CC BY 4.0. 链接的来源资料保留其自身权利。

机器访问