I have argued that there ought to be a standard function for this. There is a proposal <https://golang.org/issue/19367> to add a simpler API for manipulating slice headers directly, but it is labeled Go 2.
In the meantime, see this comment <https://golang.org/issue/13656#issuecomment-303216308> for what I believe is a portable API in Go 1. On Friday, November 3, 2017 at 12:21:59 PM UTC-4, Andy Balholm wrote: > > Use the arr1 style when the length of the C array is known at compile > time, and the arr2 style when it’s not known till runtime. > > Andy > > On Nov 3, 2017, at 9:18 AM, DV <dimiter....@gmail.com <javascript:>> > wrote: > > Which "style" of wrapping a C array in a Go slice is more idiomatic in > this code - https://play.golang.org/p/6EbKl22MPQ - "arr1" or "arr2"? They > both seem to produce the exact same result. > > I've seen the "arr2" style more often, but I don't understand why. The way > I wrapped "arr1" seems more clear to me - you have to know the length of > the C array, so why not just declare the pointer to the array with the > correct length to begin with? Why is the 1<<30 bit the more accepted style, > e.g. here - cgo > <https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices>. It > seems a bit noisy, especially with having to remember to put the length > *and* capacity when slicing the array. > > I have a feeling I'm missing something important but I don't understand > what... > > -- > 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...@googlegroups.com <javascript:>. > For more options, visit https://groups.google.com/d/optout. > > > -- 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.