In the first example, make does not escape the scope of the for statement.
In the second example, make is assigned to m, which is outside of the scope
of the for statement, which means the make operation escapes its scope and
subsequently, is heap allocated. If you want more information about why
something escapes, try compiling with -gcflags "-m -m" for an explanation
of the escape analysis information.

On Wed, Sep 28, 2016 at 7:56 AM, 刘桂祥 <liuguixiang...@gmail.com> wrote:

> go 1.7
>
> 在 2016年9月28日星期三 UTC+8下午10:41:09,Dave Cheney写道:
>
>> Which version of Go?
>>
>> On Thursday, 29 September 2016 00:18:29 UTC+10, 刘桂祥 wrote:
>>>
>>> // example1.go
>>> package main
>>>
>>>
>>> func main() {
>>>
>>>     for i := 0; i < 2; i++ {
>>>         m := make(map[int]int)
>>>
>>>
>>>         m[1] = 100
>>>     }
>>>
>>>
>>> }
>>>
>>>
>>> main make(map[int]int) does not escape
>>>
>>>
>>> // example2.go
>>> package main
>>>
>>>
>>> func main() {
>>>     var m map[int]int
>>>     for i := 0; i < 2; i++ {
>>>         m = make(map[int]int)
>>>
>>>
>>>         m[1] = 100
>>>     }
>>>
>>>
>>> }
>>>
>>>  make(map[int]int) escapes to heap    why ???
>>>
>>>
>>> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to