If I have a func like this:
func AcceptsAnything(v ...interface{}){
args =: FlattenDeep(v); // flatten any arrays or slices
}
I am trying to implement FlattenDeep:
func getKind(v interface{}) string {
rt := reflect.TypeOf(v)
switch rt.Kind() {
case reflect.Slice:
return "slice"
case reflect.Array:
return "array"
default:
return "unknown"
}
}
func FlattenDeep(args ...interface{}) []interface{} {
list := []interface{}{}
for _, v := range args {
kind := getKind(v);
if kind != "unknown" {
list = append(list, FlattenDeep(v)...) // does not compile
} else{
list = append(list, v);
}
}
return list;
}
but I don't know how to append multiple items to the list at once. Should I just loop over the results of FlattenDeep or is there a way to spread the results and append them to the list?
This might work:
func FlattenDeep(args ...interface{}) []interface{} {
list := []interface{}{}
for _, v := range args {
kind := getKind(v);
if kind != "unknown" {
for _, z := range FlattenDeep((v.([]interface{})...) {
list = append(list, z)
}
} else {
list = append(list, v);
}
}
return list;
}
but I am looking for something a little less verbose if possible