Created
August 7, 2019 00:31
-
-
Save ffrizzo/1c7c98dac1aee6cee0252d5ded34c0b4 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" | |
) | |
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