* sno <farleyschae...@gmail.com> [170808 20:57]:
> package main
> import (
> "fmt"
> "reflect"
> "unsafe"
> )
> func main() {
> a := []byte("1234567")
> printSliceDetails(a)
> }
> func printSliceDetails(a []byte) {
> sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&a))
> fmt.Printf("Backing array location: %v, data: %s, len: %d, cap: %d\n",
> sliceHeader.Data, string(a), len(a), cap(a))
> // fmt.Printf("Backing array location: %v, data: %s, len: %d, cap: %d\n",
> // sliceHeader, string(a), len(a), cap(a))
> }
> In the above code, the capacity prints 32. However, if you comment the 
> first print statement and remove the commenting on the second and run the 
> program again the capacity will only be 8. What is happening with 
> sliceHeader.Data to make its capacity grow like that?

I'm not an expert, but my guess is that the compiler is doing escape
analysis and allocating a on the heap in the first case and on the stack
in the second case (with different minimum allocation sizes).

Hopefully someone else can give a more authoritative answer.


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