Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
a := []int{1, 2, 3, 4}
b := a[1:3]
b[0] = 9
fmt.Println(a, len(b))
}[1 9 3 4] 2
- Expecting
ato stay[1 2 3 4], as ifa[1:3]made a copy: a sub-slice shares the array witha, sob[0] = 9writes intoa[1]. - Expecting
len(b)to be3: ina[1:3]the end index is excluded, sobholds the elements at indexes 1 and 2.
- How would you get a
bwhose changes do not reacha? - What would
b[2] = 9do in this program?