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

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

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容