Slice to array using the same buffer?

70 views
Skip to first unread message

Amit Lavon

unread,
Feb 1, 2021, 7:38:12 AM2/1/21
to golang-nuts
I know it's possible to create a slice variable from an array with the [:] syntax, and it points to the same underlying buffer.
Can one do the opposite? Given a slice, create an array variable based on the same buffer?

Amit

Axel Wagner

unread,
Feb 1, 2021, 7:43:35 AM2/1/21
to Amit Lavon, golang-nuts
Not yet, but probably an go 1.17: https://github.com/golang/go/issues/395

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/a469ec42-4197-4763-a155-ad64190ac996n%40googlegroups.com.

Amit Lavon

unread,
Feb 1, 2021, 7:55:10 AM2/1/21
to Axel Wagner, golang-nuts
Thanks! Something to look forward to.

Jan Mercl

unread,
Feb 1, 2021, 7:55:51 AM2/1/21
to Amit Lavon, golang-nuts
On Mon, Feb 1, 2021 at 1:38 PM Amit Lavon <amitl...@gmail.com> wrote:

> I know it's possible to create a slice variable from an array with the [:] syntax, and it points to the same underlying buffer.
> Can one do the opposite? Given a slice, create an array variable based on the same buffer?

Arrays are values so they cannot share memory with other arrays/slices.

But you can create a pointer to an array that will share the memory.
Currently only using unsafe: https://play.golang.org/p/0JggbzO4ixT
Reply all
Reply to author
Forward
0 new messages