Appending to slices

August 29, 2024

Appending to and copying slices

The built-in functions append and copy assist in common slice operations. For both functions, the result is independent of whether the memory referenced by the arguments overlaps.

Today and tomorrow we’ll look at the first of those two, append:

The variadic function append appends zero or more values x to a slice s and returns the resulting slice of the same type as s. The core type of s must be a slice of type []E. The values x are passed to a parameter of type ...E and the respective parameter passing rules apply. As a special case, if the core type of s is []byte, append also accepts a second argument with core type bytestring followed by .... This form appends the bytes of the byte slice or string.

append(s S, x ...E) S  // core type of S is []E

Alright. So that’s a lot of text to say what “mostly” boils down to:

append takes an arbitrary slice as its first argument, then zero or more additional arguments, of the appropriate type, and returns a new slice that contians the argument(s) appended to the original slice.

One special case is mentioned: If the first argument is a byte slice (i.e. []byte), the following argument may be a string, followed by .... Without the exception, only []byte would be permitted.

bs := []byte{'H', 'e', 'l', 'l', 'o', ',', ' '}
bs = append(bs, "world"...)
fmt.Println(string(bs))  // Prints: Hello, world

(See it in the playground.)

Without this exception, the above code would have to be written as follows (which, of course, still works):

bs := []byte{'H', 'e', 'l', 'l', 'o', ',', ' '}
bs = append(bs, []byte("world")...)
fmt.Println(string(bs))

Quotes from The Go Programming Language Specification Language version go1.23 (June 13, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Beware of array reuse with append

On Friday we looked at what happens when appending to a slice exceeds the capacity of the backing array. Today we’ll consider the alternative: What if the backing array can hold the new values? Appending to and copying slices … Otherwise, append re-uses the underlying array. Okay. That sounds pretty simple, doesn’t it? What’s to talk about? Well, this single sentence packs a lot of surprises. Consider this code: a := [.


Efficiently appending to slices

Yesterday we were introduced to the append function, which on the surface seems quite straight forward. But there are nuänces and gotchas! Appending to and copying slices … If the capacity of s is not large enough to fit the additional values, append allocates a new, sufficiently large underlying array that fits both the existing slice elements and the additional values. You may recall from our (much) earlier discussion about slices and backing arrays, that a slice has not only a length (the number of elements it contains), but also a capacity—essentially the number of available elements in the backing array.


Clear

Clear The built-in function clear takes an argument of map, slice, or type parameter type, and deletes or zeroes out all elements [[Go 1.21(https://go.dev/ref/spec#Go_1.21)]]. Call Argument type Result clear(m) map[K]T deletes all entries, resulting in an empty map (len(m) == 0) clear(s) []T sets all elements up to the length of s to the zero value of T clear(t) type parameter see below clear is a relatively recent addition to Go, added just over a year ago.

Get daily content like this in your inbox!

Subscribe