Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array to slice in Go

Tags:

go

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).

like image 416
FuriousGeorge Avatar asked Mar 05 '15 19:03

FuriousGeorge


People also ask

How do you slice an array in Go?

creating a slice using make func make([]T, len, cap) []T can be used to create a slice by passing the type, length and capacity. The capacity parameter is optional and defaults to the length. The make function creates an array and returns a slice reference to it.

What is difference between array and slice in Golang?

Slices in Go and Golang The basic difference between a slice and an array is that a slice is a reference to a contiguous segment of an array. Unlike an array, which is a value-type, slice is a reference type. A slice can be a complete array or a part of an array, indicated by the start and end index.

How do you slice in Golang?

Slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. It is just like an array having an index value and length, but the size of the slice is resized they are not in fixed-size just like an array.


Video Answer


4 Answers

This should work:

func Foo() [32]byte {
    return [32]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
}

func Bar(b []byte) {
    fmt.Println(string(b))
}

func main() {
    x := Foo()
    Bar(x[:])
}

And it doesn't create a copy of the underlying buffer

like image 138
Not_a_Golfer Avatar answered Oct 15 '22 19:10

Not_a_Golfer


arr[:]  // arr is an array; arr[:] is the slice of all elements
like image 44
jameshfisher Avatar answered Oct 15 '22 17:10

jameshfisher


This will do the trick:

slice := array[0:len(array)]

Also avoids copying the underlying buffer.

like image 31
bowsky Avatar answered Oct 15 '22 19:10

bowsky


You can generally slice an array by its bounds with : :

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

More generally, for the following array :

var my_array [LENGTH]TYPE

You can produce the slice of different sizes by writing :

my_array[START_SLICE:END_SLICE]

Omitting START_SLICE if it equals to the low bound and END_SLICE if equals to the high bound, in your case :

a[0:32] 

Produces the slice of the underlying array and is equivalent to :

a[0:]
a[:32]
a[:]
like image 34
Ismail H Avatar answered Oct 15 '22 18:10

Ismail H