Skip to content

Instantly share code, notes, and snippets.

@kuzemkon
Created August 24, 2017 12:13
Show Gist options
  • Save kuzemkon/7ce1dddde8b3031b3fc469f394d28c59 to your computer and use it in GitHub Desktop.
Save kuzemkon/7ce1dddde8b3031b3fc469f394d28c59 to your computer and use it in GitHub Desktop.
Golang: parsing list from the interface
func listFromInterface(x interface{})([]interface{}, error) {
s := reflect.ValueOf(x);
fmt.Println(s)
if valueType := s.Kind(); valueType != reflect.Slice && valueType != reflect.Array && valueType != reflect.String {
return nil, fmt.Errorf("Given a non-list type");
}
pe := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
pe[i] = s.Index(i).Interface()
}
return pe, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment