Skip to content

Instantly share code, notes, and snippets.

@mrkaspa
Created October 5, 2017 19:48
Show Gist options
  • Save mrkaspa/9248d91712ce57b09d8c92314590d46f to your computer and use it in GitHub Desktop.
Save mrkaspa/9248d91712ce57b09d8c92314590d46f to your computer and use it in GitHub Desktop.
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