dongzuo7166 2019-03-16 17:52
浏览 53

将结构数组转换为字符串数组以显示为表格

I was watching the following package: https://github.com/olekukonko/tablewriter

And I want to try to print my struct as something like that, but I couldn't covert my array of structs into the array of strings that the package needs.

So I tried something like that:

func print(quakes []Quake) {
    var data [][]string

    for _, quake := range quakes {
        b, err := json.Marshal(quake)
        append(data, []string(b))
    }

    table := tablewriter.NewWriter(os.Stdout)
    table.SetHeader([]string{"Name", "Sign", "Rating"})

    for _, v := range newData {
        table.Append(v)
    }
    table.Render() // Send output
}

My Quake Struct:

type Quake struct {
    Googlemapref string `json:"googlemapref"`
    Segree       string `json: "degree"`
    DataUpdate   string `json: "dataUpdate"`
    MagType      string `json:"magType"`
    ObsRegion    string `json: "obsRegion"`
    Lon          string `json:"lon"`
    Source       string `json: "source"`
    Depth        int    `json:"depth"`
    TensorRef    string `json:"tensorRef"`
    Sensed       string `json:"sensed"`
    Shakemapid   string `json:"shakemapid"`
    Time         string `json:"time"`
    Lat          string `json:"lat"`
    Shakemapref  string `json:"shakemapref"`
    Local        string `json:"local"`
    Magnitud     string `json: "magnitud"`
}

Would appreciate some help since I am new in the laguage, thanks a lot

  • 写回答

1条回答 默认 最新

  • douzong5057 2019-03-16 19:50
    关注

    There are a few issues with your code. Firstly, the append function doesn't append in place so where you do append(data, []string(b)) the result is thrown away so I think you want to do data = append(data, []string(b)) instead.

    Also, doing a json.Marshal on the struct will not make a slice of strings which you try to use it as. Instead, it will produce a single string that has all of the values in such as {"googlemapref":"something","depth":10}. The tablewriter you want to use expects to take a slice of values to put in the table that match the headings (where you appear to using the example headings of "Name", "Sign", "Rating".

    You could use the reflect package like json does to populate the fields in each row but I think this would be more complexity than it is worth and you are better just filling in each row by calling the relevant fields:

    func print(quakes []Quake) {
        var data [][]string
    
        for _, quake := range quakes {
            row := []string{quake.Googlemapref, quake.Segree, strconv.Itoa(quake.Depth),...}
            data = append(data, row)
        }
    
        table := tablewriter.NewWriter(os.Stdout)
        table.SetHeader([]string{"googlemapref", "degree", "depth",...})
    
        for _, v := range newData {
            table.Append(v)
        }
        table.Render() // Send output
    }
    

    (I've left the ... for you to fill in the other fields yourself but included depth to show how to convert it to a string).

    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂