Created
October 5, 2017 19:48
-
-
Save mrkaspa/9248d91712ce57b09d8c92314590d46f to your computer and use it in GitHub Desktop.
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" | |
) | |
type any = interface{} | |
func main() { | |
arr := []any{1, 2, 3, []any{5, 6, 7, []any{9, 0}}} | |
res := flat(arr) | |
fmt.Printf("flat %v\n", res) | |
} | |
func flat(arr []any) []any { | |
acc := []any{} | |
for _, elem := range arr { | |
v := reflect.ValueOf(elem) | |
switch v.Kind() { | |
case reflect.Slice: | |
rec := flat(elem.([]any)) | |
acc = append(acc, rec...) | |
default: | |
acc = append(acc, elem) | |
} | |
} | |
return acc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment