I run the code and the two methods output the same results, all 8

在 2017年8月9日星期三 UTC+8上午8:57:07,sno写道:
>
> Hello,
>
> I was wondering if someone could help me understand what is going on in 
> the following piece of code.
>
> 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?
>
> Thanks,
> Farley
>

-- 
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