On Sunday, July 9, 2017 at 6:31:24 AM UTC+8, Santhosh Ram Manohar wrote:
>
> hello,
>
> This piece of code prints [10 0 0 0] as expected..
>
> func main() {
>         i := 10
>         p := unsafe.Pointer(&i)
>         intPtr := (*[4]byte)(p)
>         fmt.Println(*intPtr)
> }
>
> But if I convert the unsafe.Pointer to pointer to a byte slice rather than 
> an array of 4 bytes it prints an empty slice, []
>
> func main() {
>         i := 10
>         p := unsafe.Pointer(&i)
>         intPtr := (*[]byte)(p)
>         fmt.Println(*intPtr)
> }
>
> Can someone explain why its so ? thanks.
>
> -Santhosh.
>
>
>
>
The current internal slice structure used in the official Go compiler is 
like:

// slicestruct {
        elements unsafe.Pointer
        len      int // number of elements
        cap      int // capacity}

If happens that the len and cap fields are both 0 for your unsafe slice.

 

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