Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. package flatten
  2.  
  3. // FlattenArray flattens the array input, and places inputues into the array output
  4. // When a nested array is encountered, FlattenArray will recurse into itself
  5. //
  6. // In the case that an element in input is not either int or []int, FlattenArray returns nil
  7. func FlattenArray(input []interface{}) []int {
  8. var output []int
  9. for _, val := range input {
  10. switch i := val.(type) {
  11. case int:
  12. output = append(output, i)
  13. case []interface{}:
  14. output = append(output, FlattenArray(i)...)
  15. default:
  16. return nil
  17. }
  18. }
  19. return output
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement