`y` escape because the println use interface{} as its parameter, golang 
can't know in compiling time what this parameter could be use (to be copy 
to global or send to other goroutine). so it is the interface self make 
golang hard to keep variable in stack
On Wednesday, June 2, 2021 at 1:30:49 AM UTC+8 [email protected] wrote:

> Whenever you take the address of something the compiler is going to have a 
> hard time with escape analysis due to aliasing. Especially with a complex 
> function like println with varadic args. 
>
> On Jun 1, 2021, at 11:55 AM, 'Axel Wagner' via golang-nuts <
> [email protected]> wrote:
>
> 
>
> "escape" as in "the compiler's escape analysis decides to put it on the 
> heap, instead of the stack". You can compile using `-gcflags=-m` to test 
> that yourself.
>
> On Tue, Jun 1, 2021 at 6:39 PM Robert Glonek <[email protected]> wrote:
>
>> What do you mean by escape? It prints the ptr to y, like the previous 
>> prints the ptr to x. Y is the same pointer throughout, as it should be.
>>
>> On Tuesday, 1 June 2021 at 14:51:50 UTC+1 [email protected] 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 [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/5d84ba06-ec44-477d-a90e-b67dc14535fan%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/5d84ba06-ec44-477d-a90e-b67dc14535fan%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> -- 
> 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 [email protected].
>
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CAEkBMfG5B1ptrRKywuU%2BHc2kedvL06XN871pCYusst8MP6sSjg%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfG5B1ptrRKywuU%2BHc2kedvL06XN871pCYusst8MP6sSjg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b7498107-238a-47dd-a369-eaf6394cab2bn%40googlegroups.com.

Reply via email to