On Wed, Feb 28, 2018 at 1:58 PM,  <jordan9...@gmail.com> wrote:
>
> What is the best way to get an unsafe.Pointer to a []byte's underlying
> array?

unsafe.Pointer(&b[0])


> I have added this function to syscall/syscall_linux.go to support some of
> our experiments. One of the new getsockopt calls returns a variable length
> amount of data into the buffer, which has no real set size. As such,
> userspace programs will have to retry with a larger buffer if the previous
> try was not large enough.
>
> func GetsockoptString(fd, level, opt, max_size int) (string, error) {
> var arr []byte = make([]byte, max_size, max_size)
> vallen := _Socklen(max_size)
>
> // This is really gross and bad, but first entry in a slice is a pointer
> into the array
> t := (**byte)(unsafe.Pointer(&arr))
>
> err := getsockopt(fd, level, opt, unsafe.Pointer(*t), &vallen)
>
> return string(arr[:vallen]), err
> }
>
>
> This function works fine, but seems gross to me. Am I safe to always assume
> that the first entry in a slice is always a pointer into the array for all
> of Go1.x on Linux?

No.

Ian

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