Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package flatten
- // FlattenArray flattens the array input, and places inputues into the array output
- // When a nested array is encountered, FlattenArray will recurse into itself
- //
- // In the case that an element in input is not either int or []int, FlattenArray returns nil
- func FlattenArray(input []interface{}) []int {
- var output []int
- for _, val := range input {
- switch i := val.(type) {
- case int:
- output = append(output, i)
- case []interface{}:
- output = append(output, FlattenArray(i)...)
- default:
- return nil
- }
- }
- return output
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement