Back to questions
JuniorTheoryVery common
What does the program print?
package main
import "fmt"
func change(n int, p *int, s []int) {
n, *p, s[0] = 10, 20, 30
}
func main() {
a, b, s := 1, 2, []int{3}
change(a, &b, s)
fmt.Println(a, b, s[0])
}1 20 30
- Expecting
10 20 30: Go passes every argument by value, sonis a copy ofa, and writing to it leavesaat 1. - Expecting
1 20 3: the slice header is copied, but the copy points to the same array, sos[0] = 30is seen inmain.
- Why does appending to a slice inside a function sometimes not show up outside?
- How does a pointer receiver method differ from a value receiver here?