在不复制数据的情况下将内存中的[] byte转换为[32] byte

I'm looking to covert []byte to [32]byte in Go. What's the best way? I want to reference same array in-memory, so I am not copying the data

This is the best I came up with but it's copying data...

var array []byte
var array32 [32]byte
copy(array32[:], array)

I'm looking to convert []byte to [32]byte in Go. I want to reference the same [underlying] array in memory, so I am not copying the data.


You are looking to do something that is very unsafe. To reference the byte slice underlying array, use a byte array pointer. For example,

package main

import (
    "fmt"
    "unsafe"
)

func byte32(s []byte) (a *[32]byte) {
    if len(a) <= len(s) {
        a = (*[len(a)]byte)(unsafe.Pointer(&s[0]))
    }
    return a
}

func main() {
    var (
        s []byte    // slice
        a *[32]byte // pointer to array
    )

    s = make([]byte, 32)
    a = byte32(s)
    s[0], a[1] = 42, 39
    fmt.Println(s[0], s[1], s)
    fmt.Println(a[0], a[1], *a)
}

Output:

42 39 [42 39 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
42 39 [42 39 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

The language spec does not allow you to access "directly" the underlying, backing array of a slice. You can do that as you can see in peterSO's answer using package unsafe, but as its name hints: it's unsafe.

But if you are allowed to use / pass your own slices, you can have access to its backing array if you create the slice by slicing an array. This requires no magic, the slicing expressions do exactly this if applied on arrays: the resulting slice will use the slicing operand (the array) as its backing array.

For example:

var a [32]byte
s := a[:]

a[0] = 1
fmt.Println(a)
fmt.Println(s)

s[1] = 2
fmt.Println(a)
fmt.Println(s)

Output (try it on the Go Playground):

[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

a is an array and s is a slice. Modifying elements via any of them, "both" will change at the same time. s has a as its backing array, s references a.