I am trying to type cast between a slice of []int32 to a slice of []int64 in Go. Is there anyway i can convert between these types if not directly?
Asked
Active
Viewed 239 times
-3
-
4You can convert an int32 to int64 and back, but not the slices of those types. Create a new slice of needed type and convert elements one by one. – Sergio Tulentsev Nov 07 '19 at 21:01
-
1The answer to your title question is yes. The answer to your body question is no. Which is your actual question? – Adrian Nov 07 '19 at 21:01
-
2See also: https://stackoverflow.com/questions/11924196/convert-between-slices-of-different-types – Adrian Nov 07 '19 at 21:03
-
1And also: https://tour.golang.org/basics/13 – Adrian Nov 07 '19 at 21:04
1 Answers
-2
No it is not possible. The reason is that cells of the arrays backing those slices are made of either 4 or 8 bytes. Looking at 4 bytes as if it were 8 makes no sense. You need to copy the contents one by one.

Grzegorz Żur
- 47,257
- 14
- 109
- 105
-
That's why you can't assert one to the other, but has nothing to do with why you can't convert one to the other (otherwise the same would be true of int32 <-> int64, which you can convert between, and which also have different widths). – Adrian Nov 07 '19 at 21:15