Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It is not possible to directly convert a pointer to a slice of one type to a slice of a different type. However, you can manually copy the data from the original slice to a new slice of a different type using a loop or a built-in function like copy(). Here's an example:

// create a slice of integers
ints := []int{1, 2, 3, 4, 5}

// create a pointer to the slice of integers
intPtr := &ints

// create a new slice of float64s with the same length as the original slice
floats := make([]float64, len(*intPtr))

// manually copy the data from the original slice to the new slice
for i, v := range *intPtr {
    floats[i] = float64(v)
}

// prints [1 2 3 4 5] followed by [1 2 3 4 5] as float64s
fmt.Println(ints)
fmt.Println(floats)