How to convert array to slice in Go?

Answer • 1 Asked • Jan 28 2020
This seems like it would be a fairly common thing and abundant examples across the interwebs, but I can't seem to find an example of how to convert an [32]byte to []byte.

I have a function that I call from an external lib that returns an array
func Foo() [32]byte {...}

I then need to pass that result to a different function for further processing.
func Bar(b []byte) { ... }

Unforunately, if I try to call
d := Foo() Bar(d)

I get
cannot convert d (type [32]byte) to type []byte

Doing
[]byte(d)

isn't much better. How do I do this, especially without creating a copy of the data (seems silly to copy this data when all I'm doing is passing it along).

Write your answer...

On a mission to build Next-Gen Community Platform for Developers