0

I have troubles translating this piece of code, which is effectively a left rotate on a slice, into a more generic version which accepts interface{} as an input parameter.

func rotate(a *[]int, i int) {
    x, b := (*a)[:i], (*a)[i:]
    *a = append(b, x...)
}

I have troubles with the final assignment:

func rotateSlice(a interface{}, i int) {
    v := reflect.ValueOf(a)
    x, b := v.Elem().Slice(0, i), v.Elem().Slice(i, v.Elem().Len())
    *a = reflect.AppendSlice(b, x)
}

The error message is invalid indirect of a (type {}). The value of a is interface{}, hence *a = would be to assign the right-hand value to the space where the pointer is pointing to. My call to AppendSlice returns Value though. I am not sure where the type assertion needs to happen, I suppose on the left-hand side?

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143

2 Answers2

1

Eliminate the use of the reflect package in Go 1.18 and later by using generics:

func rotateSlice[T any](a []T, i int) []T {
    x, b := a[:i], a[i:]
    return append(b, x...)
}

Call it like this: x = rotateSlice(x, 2)


Here's now to implement rotate using the reflect package.

Use Value.Set to set the value in the slice.

func rotateSlice(a interface{}, i int) {
    v := reflect.ValueOf(a).Elem()
    x, b := v.Slice(0, i), v.Slice(i, v.Len())
    v.Set(reflect.AppendSlice(b, x))
}

Call the function with a pointer to a slice:

a := []string{"a", "b", "c", "d"}
roateSlice(&a, 2)

Run all of the examples on the playground.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
1

a is an interface{} not a pointer, so you can't dereference it. Even if you have a pointer to a slice, you can't assigned the result ofreflect.AppendSlice, because it returns the type reflect.Value. You need to set the value via Value.Set.

https://play.golang.org/p/JCF8jsRJ_O

func rotateSlice(a interface{}, i int) {
    v := reflect.ValueOf(a).Elem()
    x, b := v.Slice(0, i), v.Slice(i, v.Len())
    v.Set(reflect.AppendSlice(b, x))
}
JimB
  • 104,193
  • 13
  • 262
  • 255