I'm trying to convert a fixed size array [32]byte
to variable sized array (slice) []byte
:
package main
import (
"fmt"
)
func main() {
var a [32]byte
b := []byte(a)
fmt.Println(" %x", b)
}
but the compiler throws the error:
./test.go:9: cannot convert a (type [32]byte) to type []byte
How should I convert it?
Use b := a[:]
to get the slice over the array you have. Also see this blog post for more information about arrays and slices.
There are no variable-sized arrays in Go, only slices. If you want to get a slice of the whole array, do this:
b := a[:] // Same as b := a[0:len(a)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With