Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
type Point struct{ X, Y int }
func main() {
p := Point{X: 1, Y: 2}
q, r := p, &p
r.X = 9
fmt.Println(q.X, p.X)
}1 9
- Expecting
qto change too:q := pcopies every field of the struct, soqis a separatePointthat keepsXequal to1. - Expecting
r.X = 9to need(*r).X: Go dereferences a pointer to a struct for you, sor.Xwrites the field ofpitself.
- What would
fmt.Println(r.X)print at the end of the program? - How would you pass a
Pointto a function so that the function can change it?