dongyu4908 2018-12-21 01:40
浏览 49
已采纳

展平参数列表,其中参数可以是切片或数组

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

  • 写回答

1条回答 默认 最新

  • douhuifen9942 2018-12-21 02:30
    关注

    Here's how to flatten arbitrary slices and arrays to a []interface{}:

    func flattenDeep(args []interface{}, v reflect.Value) []interface{} {
    
        if v.Kind() == reflect.Interface {
            v = v.Elem()
        }
    
        if v.Kind() == reflect.Array || v.Kind() == reflect.Slice {
            for i := 0; i < v.Len(); i++ {
                args = flattenDeep(args, v.Index(i))
            }
        } else {
            args = append(args, v.Interface())
        }
    
        return args
    }
    
    func AcceptsAnything(v ...interface{}) {
        args := flattenDeep(nil, reflect.ValueOf(v))
        fmt.Println(args)
    }
    

    Run it on the Playground

    If the function must handle slice and array types with an arbitrary element type, then the application must iterate through the slice or array using the reflect API to get the values into an []interface{}.

    If you only need to flatten []interface{}, then the reflect API is not needed:

    func flattenDeep(args []interface{}, v interface{}) []interface{} {
        if s, ok := v.([]interface{}); ok {
            for _, v := range s {
                args = flattenDeep(args, v)
            }
        } else {
            args = append(args, v)
        }
        return args
    }
    
    func AcceptsAnything(v ...interface{}) {
        args := flattenDeep(nil, v)
        fmt.Println(args)
    }
    

    Run it on the Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错