I think I got the reason.
It is because the compiler is not aware of the println will not change the 
value of y.
And it is hard or expensive for the compiler to understand the channel 
synchronization.

If the println function changes y, then y must be allocated on heap.



On Tuesday, June 1, 2021 at 9:51:50 AM UTC-4 tapi...@gmail.com wrote:

>
> package main
>
> func newIntPtr(n int) *int {
>     return &n
> }
>
> func main() {
>     x := newIntPtr(3)
>     y := newIntPtr(5)
>     c := make(chan bool)
>     go func() {
>         *y++
>         close(c)
>     }()
>     <-c
>     println(*x, *y)
>     println(&x)
>     //println(&y) // This line makes y escape.
> }
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/6829f596-8801-4494-a160-de379f33aaf5n%40googlegroups.com.

Reply via email to