package main
import "fmt"
func main() {
a := []int{1, 2, 3, 4, 5}
b := append(a[:3], 6, 7)
fmt.Println("a is ", a)
fmt.Println("b is ", b)
}
The above program prints
a is [1, 2, 3, 6, 7]
b is [1, 2, 3, 6, 7]
I understand the output of b
. But why did slice a
change?
Shouldn't a[:3]
create a new anonymous slice and append 6,7 and return that to b
instead of changing the original(a
) slice? Why did append act on the original slice?