On Monday, 3 October 2016 12:39:34 UTC+11, 刘桂祥  wrote:
> package main
> 
> 
> import (
>     "fmt"
> )
> func main() {
>     a := interface{}(100)
>     println(&a, a)
>     fmt.Sprint(a)
> }
> 
> 
> go run x.go  the output:
> 
> 
> 0xc42003bf18 (0x89040,0xc42000a2c0)
> 
> 
> 
> I just want to ask 0xc42000a2c0 as the a (interface{}) 's data pointer is 
> point to where ? heap ?

It is on the heap, the value escapes when passed to fmt,Sprint

> 
> also I test this:
> 
> 
> func BenchmarkFmtEscap3(b *testing.B) {
>     b.ReportAllocs()
> 
> 
>     for i := 0; i < b.N; i++ {
>         a := interface{}(100)
>         fmt.Sprint(a)
>     }
> }
> 
> 
> BenchmarkFmtEscap3-4          10000000               141 ns/op              
> 16 B/op       2 allocs/op
> 
> 
> 
> why is 2 allocs and 16bytes ??

One for a, which escapes to the heap when passed to fmt.Sprint, and one for the 
value returned from fmt,Sprint

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

Reply via email to