You should change the title to “Go’s append is not thread safe”. It’s never thread safe. :)
I find slices and append
to be maybe the 2nd most confusing thing about go, after make
vs new
. And slices, IMHO, are very much a compromise. They try to offer a lot of functionality and flexibility, but maintain performance. From that we have the weird behavior that sometimes slices move (requiring the re-assignment), and sometimes they grow in place.
But the real problem here is violating “Do not communicate by sharing memory; instead, share memory by communicating.” (https://blog.golang.org/share-memory-by-communicating)
My understanding is that if it’s not explicitly declared as thread-safe, you’d better assume it’s not. That probably even applies to something as basic as i++
.