Skip to content

Instantly share code, notes, and snippets.

@liamg
Last active September 26, 2018 10:31
Show Gist options
  • Save liamg/4b3efa15075b93e00496e7e0aafbc168 to your computer and use it in GitHub Desktop.
Save liamg/4b3efa15075b93e00496e7e0aafbc168 to your computer and use it in GitHub Desktop.
Convert Go slice in interface{} to []interface{}
package main
import (
"fmt"
"reflect"
)
func convertInterfaceToSlice(slice interface{}) ([]interface{}, error) {
val := reflect.ValueOf(slice)
if val.Kind() != reflect.Slice {
return nil, fmt.Errorf("Cannot convert value of kind %s to slice", val.Kind().String())
}
niceSlice := make([]interface{}, val.Len())
for i := 0; i < val.Len(); i++ {
niceSlice[i] = val.Index(i).Interface()
}
return niceSlice, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment