Created
August 24, 2017 12:13
-
-
Save kuzemkon/7ce1dddde8b3031b3fc469f394d28c59 to your computer and use it in GitHub Desktop.
Golang: parsing list from the 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
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