Skip to content

Instantly share code, notes, and snippets.

@ffrizzo
Created August 7, 2019 00:31
Show Gist options
  • Save ffrizzo/1c7c98dac1aee6cee0252d5ded34c0b4 to your computer and use it in GitHub Desktop.
Save ffrizzo/1c7c98dac1aee6cee0252d5ded34c0b4 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func fList(v ...interface{}) []interface{} {
return v
}
func main() {
a := fList(1, fList(2, 3, 4), 5, 6, 8, fList(7, 8, 9))
fmt.Println(a)
fmt.Println(flatten(a))
}
func flatten(s []interface{}) []int {
result := []int{}
for _, v := range s {
switch i := v.(type) {
case int:
result = append(result, i)
case []interface{}:
result = append(result, flatten(i)...)
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment