Last active
September 26, 2018 10:31
-
-
Save liamg/4b3efa15075b93e00496e7e0aafbc168 to your computer and use it in GitHub Desktop.
Convert Go slice in interface{} to []interface{}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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