Assign result of reflect.AppendSlice to pointer

Issue

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?

Solution

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

Answered By – JimB

Answer Checked By – Timothy Miller (GoLangFix Admin)

Leave a Reply

Your email address will not be published.