Go Subtle Bug: `append()` Overwrote Elements of Another Slice Sharing Backing Array

Sub-slice retained spare capacity (`cap > len`), so `append` mutated the parent slice.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Go Subtle Bug Platform: Go
Common Root Causes:
  • Sub-slice retained spare capacity (`cap > len`), so `append` mutated the parent slice.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Use full three-index slice expression `s[a:b:b]` or `slices.Clone()` to force a fresh backing array.
Safe Diagnostic Command:
Inspect sub-slice expressions `s[a:b]`
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Sub-slice retained spare capacity (`cap > len`), so `append` mutated the parent slice. Recommended diagnostic: `Inspect sub-slice expressions `s[a:b]``. Remediation: Use full three-index slice expression `s[a:b:b]` or `slices.Clone()` to force a fresh backing array.

Worked Examples

  • Run `Inspect sub-slice expressions `s[a:b]`` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-go-slice-append-aliasing-bug · Independently verified: 2026-11-25

Related Resources