Foundations
Slices
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
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)
}
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 := 0total ← 82
pass 1 of 310for _, score82 := range scores[]int{82, 91, 76} {11 total→ 82 += score8212}All 3 passes — pass 1 is the card above pass scoretotal1 82 0 → 82 2 91 82 → 173 3 76 173 → 249 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
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 := 0total ← 100
pass 1 of 310for _, score100 := range scores[]int{100, 95, 90} {11 total→ 100 += score10012}All 3 passes — pass 1 is the card above pass scoretotal1 100 0 → 100 2 95 100 → 195 3 90 195 → 285 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
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 := 0total ← 60
pass 1 of 310for _, score60 := range scores[]int{60, 70, 80} {11 total→ 60 += score6012}All 3 passes — pass 1 is the card above pass scoretotal1 60 0 → 60 2 70 60 → 130 3 80 130 → 210 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
scoresstarts as[]int{82, 91, 76}.firstScore := scores[0]reads82.- The loop adds
82 + 91 + 76. totalbecomes249.- Integer division makes
averageequal83, so the program printsfirst= 82andaverage= 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.