将结构切片转换为字符串的二维切片

我想从数据库中获取数据并写入 excel


假设我有一个像这样的结构:


type user struct {

    ID    int64

    Name  string

    Age   int

}

我可以获得指向用户类型表单 DB 的切片的指针&[]user{}


但我想将该切片转换为字符串的二维切片[][]string{}


这是我的代码尝试做这样的工作:


func toStrings(slice interface{}) [][]string {

    switch reflect.TypeOf(slice).Elem().Kind() {

    case reflect.Slice:

        ret := [][]string{}

        val := reflect.ValueOf(slice).Elem()

        for i := 0; i < val.Len(); i++ {


            tempSlice := []string{}


            tempV := reflect.ValueOf(val.Index(i))


            for j := 0; j < tempV.NumField(); j++ {

                tempSlice = append(tempSlice, tempV.Field(j).String())

            }


            ret = append(ret, tempSlice)

        }

        return ret

    }

    return nil


}

但是从上面的代码中我得到的是一个像[<*reflect.rtype Value> <unsafe.Pointer Value> <reflect.flag Value>]


我哪里做错了?


缥缈止盈
浏览 147回答 3
3回答

HUX布斯

对不起,我发现我做错了,我tempV错了func toStrings(slice interface{}) [][]string {&nbsp; &nbsp; switch reflect.TypeOf(slice).Elem().Kind() {&nbsp; &nbsp; case reflect.Slice:&nbsp; &nbsp; &nbsp; &nbsp; ret := [][]string{}&nbsp; &nbsp; &nbsp; &nbsp; val := reflect.ValueOf(slice).Elem()&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < val.Len(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempSlice := []string{}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // tempV should be:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempV := val.Index(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // instead of reflect.ValueOf(val.Index(i))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for j := 0; j < tempV.NumField(); j++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempSlice = append(tempSlice, tempV.Field(j).String())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret = append(ret, tempSlice)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return ret&nbsp; &nbsp; }&nbsp; &nbsp; return nil}

千巷猫影

也许我有一个简单的方法来解决问题,golang playground here我曾经encoding/json转换为json数据,然后将其转换为map[string]interface{}。func toStrings2(slice interface{}) [][]string {&nbsp; &nbsp; jsonData, _ := json.Marshal(slice)&nbsp; &nbsp; var out []map[string]interface{}&nbsp; &nbsp; _ = json.Unmarshal(jsonData, &out)&nbsp; &nbsp; var fields []string&nbsp; &nbsp; if len(out) > 0 {&nbsp; &nbsp; &nbsp; &nbsp; for k := range out[0] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fields = append(fields, k)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; var ret [][]string&nbsp; &nbsp; for _, row := range out {&nbsp; &nbsp; &nbsp; &nbsp; var r []string&nbsp; &nbsp; &nbsp; &nbsp; for _, k := range fields {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r = append(r, fmt.Sprint(row[k]))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ret = append(ret, r)&nbsp; &nbsp; }&nbsp; &nbsp; return ret}注意:在@CeriseLimón 的帮助下,我知道此答案中的代码无法处理 User.ID 的大值。

慕田峪7331174

问题中的代码有两个问题。第一个问题是 slice 元素被表达式中的 aa reflect.Value 双重包裹reflect.Value(val.Index(i))。通过删除对 reflect.Value 的额外调用来修复。第二个问题是reflect.Value String方法不会将基础值转换为其字符串表示形式。使用 fmt.Sprint(或其朋友之一)来做到这一点。试试这个:func toStrings(slice interface{}) [][]string {&nbsp; &nbsp; // Get reflect value for slice. Use Indirect to&nbsp; &nbsp; // handle slice argument and pointer to slice&nbsp; &nbsp; // argument.&nbsp; &nbsp; v := reflect.Indirect(reflect.ValueOf(slice))&nbsp; &nbsp; if v.Kind() != reflect.Slice {&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; var result [][]string&nbsp; &nbsp; // For each element...&nbsp; &nbsp; for i := 0; i < v.Len(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; // Get reflect value for slice element (a struct). Use&nbsp; &nbsp; &nbsp; &nbsp; // Indirect to handle slice of struct and slice of&nbsp; &nbsp; &nbsp; &nbsp; // pointer to struct.&nbsp; &nbsp; &nbsp; &nbsp; e := reflect.Indirect(v.Index(i))&nbsp; &nbsp; &nbsp; &nbsp; if e.Kind() != reflect.Struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Convert fields to string and append.&nbsp; &nbsp; &nbsp; &nbsp; var element []string&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < e.NumField(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use fmt.Sprint to convert arbitrary Go value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // to a string.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; element = append(element, fmt.Sprint(e.Field(i).Interface()))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; result = append(result, element)&nbsp; &nbsp; }&nbsp; &nbsp; return result}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go