Go slices store ordered values and work well with loops and indexing.

slice A slice is a flexible view over ordered values, written with syntax such as `[]int{1, 2, 3}`.

Slices

scores
slices.go
Replay: real traced execution (multi-file project)
package main

import "fmt"

func main() {
	var scores = []int{82, 91, 76}
	firstScore := scores[0]
	total := 0

	for _, score := range scores {
		total += score
	}

	average := total / len(scores)
	fmt.Println("first=", firstScore)
	fmt.Println("average=", average)
}
package main

import "fmt"

func main() {
	var scores = []int{100, 95, 90}
	firstScore := scores[0]
	total := 0

	for _, score := range scores {
		total += score
	}

	average := total / len(scores)
	fmt.Println("first=", firstScore)
	fmt.Println("average=", average)
}
package main

import "fmt"

func main() {
	var scores = []int{60, 70, 80}
	firstScore := scores[0]
	total := 0

	for _, score := range scores {
		total += score
	}

	average := total / len(scores)
	fmt.Println("first=", firstScore)
	fmt.Println("average=", average)
}
  1. scores ← []int{82, 91, 76}, firstScore ← 82, total ← 0

    5func main() {6  var scores→ []int{82, 91, 76} = []int{82, 91, 76} //@scores=[]int{100, 95, 90}, []int{60, 70, 80}7  firstScore→ 82 := scores[0]828  total→ 0 := 0
  2. total ← 82

    pass 1 of 3
    10for _, score82 := range scores[]int{82, 91, 76} {11  total→ 82 += score8212}
    All 3 passes — pass 1 is the card above
    passscoretotal
    1820 82
    29182 173
    376173 249
  3. average ← 83

    14  average→ 83 := total249 / len(scores[]int{82, 91, 76})15  fmt.Println("first=", firstScore82)16  fmt.Println("average=", average83)17}
    outputfirst= 82
    average= 83
  1. scores ← []int{100, 95, 90}, firstScore ← 100, total ← 0

    5func main() {6  var scores→ []int{100, 95, 90} = []int{100, 95, 90}7  firstScore→ 100 := scores[0]1008  total→ 0 := 0
  2. total ← 100

    pass 1 of 3
    10for _, score100 := range scores[]int{100, 95, 90} {11  total→ 100 += score10012}
    All 3 passes — pass 1 is the card above
    passscoretotal
    11000 100
    295100 195
    390195 285
  3. average ← 95

    14  average→ 95 := total285 / len(scores[]int{100, 95, 90})15  fmt.Println("first=", firstScore100)16  fmt.Println("average=", average95)17}
    outputfirst= 100
    average= 95
  1. scores ← []int{60, 70, 80}, firstScore ← 60, total ← 0

    5func main() {6  var scores→ []int{60, 70, 80} = []int{60, 70, 80}7  firstScore→ 60 := scores[0]608  total→ 0 := 0
  2. total ← 60

    pass 1 of 3
    10for _, score60 := range scores[]int{60, 70, 80} {11  total→ 60 += score6012}
    All 3 passes — pass 1 is the card above
    passscoretotal
    1600 60
    27060 130
    380130 210
  3. average ← 70

    14  average→ 70 := total210 / len(scores[]int{60, 70, 80})15  fmt.Println("first=", firstScore60)16  fmt.Println("average=", average70)17}
    outputfirst= 60
    average= 70

Follow the Slice

  1. scores starts as []int{82, 91, 76}.
  2. firstScore := scores[0] reads 82.
  3. The loop adds 82 + 91 + 76.
  4. total becomes 249.
  5. Integer division makes average equal 83, so the program prints first= 82 and average= 83. | scores | first | total | average | | --- | --- | --- | --- | | 100, 95, 90 | 100 | 285 | 95 | | 82, 91, 76 | 82 | 249 | 83 | | 60, 70, 80 | 60 | 210 | 70 |

Exercise: slices.go

Reproduce first= 82 and average= 83, then try one listed score slice and predict its first score and average.