Go Optimizations 101, Go Details & Tips 101 and Go Generics 101 are all updated for Go 1.25 now.

Black Friday Special! You can grab 4 "Go 101" books in this bundle on Leanpub for as low as $11.99 for a limited time (It is $57.96 without discount).

slice 2

What does the following program print?

package main

func main() {
	var x = []string{"A", "B", "C"}

	for i, s := range x {
		print(i, s, ",")
		x[i+1] = "M"
		x = append(x, "Z")
		x[i+1] = "Z"
	}
}

Choices:

Answer: 0A,1M,2C,

Run it on Go play.

Key points:
  • Ranging over a container iterates the elements of a copy of the container. The evaluation of the copy happens before executing the loop, so the length and capacity of the copy are never changed.
  • A slice references its elements on a backing array. So a copy of a slice shares the same elements (and the backing array) with the slice.
  • The assignment x[i+1] = "M" in the first loop step modifies the second element of the initial slice x and the copy of the initial slice x.
  • If the free element slots of the first argument slice of an append call are insufficient to hold all the appended elements, the append call will create a new backing array to hold all the elements of the first argument slice and the appended elements. So, at the end of the first loop step, the backing array of the slice x is changed. However, the change doesn't affect the slice copy used in the element iteration. All subsequent element modifications apply to the new backing array, so they have no effects on the copy used in the element iteration.

This above quiz is extended from one of Valentin Deleplace's quizzes. The following is the original quiz.

package main

func main() {
	var x = []string{"A", "B", "C"}

	for i, s := range x {
		print(i, s, ",")
		x = append(x, "Z")
		x[i+1] = "Z"
	}
}

The original quiz prints 0A,1B,2C,, because the ranged container is a copy of the initial x, and elements of the copy are never changed.

The following is another quiz made by Valentin Deleplace (with a bit modification).

package main

func main() {
	var y = []string{"A", "B", "C", "D"}
	var x = y[:3]

	for i, s := range x {
		print(i, s, ",")
		x = append(x, "Z")
		x[i+1] = "Z"
	}
}

The other quiz prints 0A,1Z,2C,. It is similar to the above extended quiz. Key points:

  • The first append call doesn't create a new backing array, so the assignment x[i+1] = "Z" in the first loop has effect on the iniital slice x (and its copy used in the element iteration).
  • The second append call creates a new backing array, so subsequent x[i+1] = "Z" assignments have no effects on the iniital slice x (and its copy used in the element iteration).


(more articles ↡)

The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.

Tapir, the author of Go 101, has been on writing the Go 101 series books and maintaining the go101.org website since 2016 July. New contents will be continually added to the book and the website from time to time. Tapir is also an indie game developer. You can also support Go 101 by playing Tapir's games (made for both Android and iPhone/iPad):
Individual donations via PayPal are also welcome.

Articles in this book:

(more are coming ...)