Go Slices Pitfalls

The cluster focuses on the quirks, bugs, and counterintuitive behaviors of slices in Go, including shared underlying arrays, append reallocation effects, passing by value with hidden references, and comparisons to other languages.

➡️ Stable 0.6x Programming Languages
2,058
Comments
18
Years Active
5
Top Authors
#7963
Topic ID

Activity Over Time

2009
3
2010
2
2011
8
2012
37
2013
99
2014
74
2015
134
2016
94
2017
126
2018
132
2019
139
2020
87
2021
265
2022
203
2023
226
2024
194
2025
232
2026
3

Keywords

e.g CLI OK NewReader gunzip.go L11 UB IMO node.go twitter.com slices slice reference append pointer arrays array passed func language

Sample Comments

SamReidHughes May 16, 2015 View on HN

Have you used Go and ran into problems with people overwriting data in slices underneath you? Was it to such a degree that it took precedence over other bugs and slowed down development in the effort to catch these mistakes manually?

kbolino Feb 5, 2025 View on HN

This isn't really a practical problem.If you created the slice, you control it and you can modify its elements, append to it, etc.If you didn't create the slice, you don't control it. If you want to modify it or append to it, you should copy it first.This has reflected how I've seen Go used professionally by people experienced in the language. The language could offer more help (but probably never will), but it's not hardly a regression from C. The real r

jerryclinesmith May 17, 2020 View on HN

100% agree - slices are a very leaky abstraction in Go

Thaxll Jan 19, 2024 View on HN

Indeed: https://go.googlesource.com/go/+/master/src/runtime/slice.go...

krallja Mar 3, 2024 View on HN

There's a layer of pointer indirection when using slices in Go, you may be able to eke out some time by moving to arrays on the stack.

skitter Mar 10, 2023 View on HN

Go has it's own fundamental problems. Something very basic that turns me off is what happens to the original slice when appending to a it (done by calling append(slice, item) and using the result). Does it get modified? It may or it may not. Either one would be useful, but this way nobody else can hold a reference to the slice because it becomes useless to them.

skybrian Oct 11, 2019 View on HN

Yes, that's not good, but it seems like in practice this doesn't come up too often in Go?More typically you're building a new slice containing the results of a query or other computation, and returning it without keeping a reference. Or instead of exposing an internal map, you have a Get method.

erik_seaberg Aug 12, 2024 View on HN

Maps and channels and functions are passed by reference. Slices are passed and returned by value but sometimes share state invisibly, the worst of both worlds. It would make more sense if Go either made this stuff immutable, made defensive copies, or refused and required using explicit pointers for all these cases.

tucnak Oct 28, 2015 View on HN

I am afraid there isn't. In fact, it's called a "slice trick" and listed in Go wiki: https://github.com/golang/go/wiki/SliceTricks

mjevans Jul 15, 2024 View on HN

Golang's slices view of the world is addictive.