Why you need to avoid using append in Go

append is the go to function when you are adding an element to a slice. But is it the best way to do so?

Short answer, no it isn’t.

Here’s why…

First, let’s create two functions that adds or assigns "x" to a string slice.

  1. WithAppend calls append to add "x" to a string slice. This is probably the most straightforward method.
func WithAppend() []string {
var l []string
for i := 0; i < 100; i++ {
l = append(l, "x")
}

return l
}