Back to questions
JuniorTheoryCommon
What does the program print?
package main
import "fmt"
func main() {
x := 1
p := &x
y := *p
*p = 5
fmt.Println(x, y)
}5 1
- Expecting
xto stay1:p := &xstores the address ofx, not a copy, so*p = 5writes intoxitself. - Expecting
yto followx:y := *pcopies the value1thatppoints to at that moment, soystays1.
- What would the program print if the line were
p = &yinstead ofy := *p? - What is the type of
p, and why can it not hold the address of astring?