doulupian8725 2019-01-22 04:23
浏览 203
已采纳

如何将多维切片[] interface {}转换为切片[] string?

Example source:

// source multidimensional slice
var source = []interface{}{
    "value1",
    "value2",
    1234,
    1234.1234,
    []int{222, 333},
    []float32{444.444, 555.555},
    []interface{}{555, "value4", []int{777, 888}}
}

Target:

// target []string
var target = []string{
    "value1",
    "value2",
    "1234",
    "1234.1234",
    "222",
    "333",
    "444.444",
    "555.555",
    "555",
    "value4",
    "777",
    "888"
}

I wrote the conversion function. But it seems to me cumbersome and it does not cover all possible options. Could you tell me please can be there is more elegant decision?

// convert multidimensional slice []interface{} to slice []string
func convert(raw interface{}) (result []string) {
    switch raw.(type) {
    case []string:
        result = append(result, raw.([]string) ...)
    case []int:
        for _, elem := range raw.([]int) {
            result = append(result, convert(elem) ...)
        }
    case string, int, int8, int16, int32, int64, float32, float64, complex64, complex128:
        result = append(result, fmt.Sprintf("%v", raw))
    case []float32:
        for _, elem := range raw.([]float32) {
            result = append(result, convert(elem) ...)
        }
    case []rune:
        for _, elem := range raw.([]rune) {
            result = append(result, convert(elem) ...)
        }
    case []int8:
        for _, elem := range raw.([]int8) {
            result = append(result, convert(elem) ...)
        }
    case []int16:
        for _, elem := range raw.([]int16) {
            result = append(result, convert(elem) ...)
        }
    case []int64:
        for _, elem := range raw.([]int64) {
            result = append(result, convert(elem) ...)
        }
    case []float64:
        for _, elem := range raw.([]float64) {
            result = append(result, convert(elem) ...)
        }
    case []complex64:
        for _, elem := range raw.([]complex64) {
            result = append(result, convert(elem) ...)
        }
    case []complex128:
        for _, elem := range raw.([]complex128) {
            result = append(result, convert(elem) ...)
        }
    case []interface{}:
        for _, elem := range raw.([]interface{}) {
            result = append(result, convert(elem) ...)
        }
    case [][]interface{}:
        for _, elem := range raw.([][]interface{}) {
            result = append(result, convert(elem) ...)
        }
    default:
        fmt.Println(fmt.Sprintf("Unknown type %v", raw))
        os.Exit(1)
    }
    return result
}
  • 写回答

1条回答 默认 最新

  • dousuize3082 2019-01-22 04:53
    关注

    Use the reflect package to handle all types of slices in a few lines of code:

    func convert(dst []string, v reflect.Value) []string {
        // Drill down to the concrete value
        for v.Kind() == reflect.Interface {
            v = v.Elem()
        }
    
        if v.Kind() == reflect.Slice {
            // Convert each element of the slice.
            for i := 0; i < v.Len(); i++ {
                dst = convert(dst, v.Index(i))
            }
        } else {
            // Convert value to string and append to result.
            dst = append(dst, fmt.Sprint(v.Interface()))
        }
        return dst
    }
    

    Call it like this:

    stringSlice := convert(nil, reflect.ValueOf(source))
    

    Run it on the Playground

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记