Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
a := []int{10, 20, 30, 40}
b := a[1:2:2]
b = append(b, 5)
b[0] = 1
fmt.Println(a, b)
}[10 20 30 40] [1 5]
- Expecting
[10 1 5 40]fora: the third index caps the capacity ofbat 1, soappendcopiesbinto a new array and leavesaalone. - Expecting
b[0] = 1to reacha: afterappendmovesbto a new array, the two slices no longer share any element.
- What would the program print with
b := a[1:2]instead? - When would you hand out
s[i:j:j]rather thans[i:j]from a function?