You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

753 B

title date categories draft
Gotcha - Up-ending a backing array 2019-09-26T21:42:18-07:00 [gotcha] true

To give better context to the problem being presented in this article, we must go over how slices work.

A slice value consists of three parts: a pointer to a backing array, a length and a capacity.

Excerpt of runtime/slice.go from the standard library:


type slice struct {
	array unsafe.Pointer
	len   int
	cap   int
}

When calling append on a slice whose length is the same as its capacity, a new backing array of greater size is created. The previous backing array's contents are copied.

A range of elements from a slice may be selected like so:

bar := foo[4:7]

When calling append on such a slice,