golang / tour

[mirror] A Tour of Go
BSD 3-Clause "New" or "Revised" License
1.53k stars 521 forks source link

tour: avoid re-using the same variable as it doesn't give the expected results #1614

Closed sagaritrockz closed 3 weeks ago

sagaritrockz commented 1 month ago

Context: https://go.dev/tour/moretypes/10

Change the title above to describe your issue and add your feedback here, including code if necessary

Change this ` func main() { s := []int{2, 3, 5, 7, 11, 13}

s = s[1:4]
fmt.Println(s)

s = s[:2]
fmt.Println(s)

s = s[1:]
fmt.Println(s)

} `

to this ` func main() { s := []int{2, 3, 5, 7, 11, 13}

s1 := s[1:4]
fmt.Println(s1)

s2 := s[:2]
fmt.Println(s2)

s3 := s[1:]
fmt.Println(s3)

} `

ALTree commented 3 weeks ago

I think the point is also to show the progressive modification of the original slice, so it's okay to re-use the same variable.