dongzhong8834 2019-06-15 05:18
浏览 35

无法弄清楚如何格式化循环打印输出的值

I am trying to change the output of a subcommand for a CLI plugin but struggling to get it to work correctly. Currently it shows an ugly printed list; I want to format it into a nice table that's easier to read. Without the table, it works fine...just looks ugly. Adding in the table breaks it by saying "....used as a value".

I have tried declaring the line as a string array with no luck. I'm missing something and not sure what.

    for {
            select {
            case <-ticker.C:
                stats, _ := ccclient.AppStats(app.Metadata.GUID)
                table := ui.Table([]string{"Sample Time", "Instance ID", "CPU %", "Memory (MB)", "Disk (MB)", "Cell IP:port"})
                for key, value := range stats {
                    tickerObject := []string{time.Now().Format(time.RFC3339), key}
                    //ui.Say(fmt.Sprintf("%s, %s, %s, %s, %s, %s", time.Now().Format(time.RFC3339), key, fmt.Sprintf("%f", value.Stats.Usage.CPU), fmt.Sprintf("%f", (value.Stats.Usage.Mem/1024/1024)), fmt.Sprintf("%f", (value.Stats.Usage.Disk/1024/1024)), value.Stats.Host+":"+strconv.Itoa(value.Stats.Port)))}
                    table.Add(tickerObject)
                    table.Print()
                }
            case <-quit:
                ticker.Stop()
            }
        }

I expect a formatted output that looks cleaner, instead I get -

cannot use tickerObject (type []string) as type string in argument to table.Add

Current output (using the commented out ui.Say line), does not line up:

Collecting stats on app fabric-tasks in space Functions Dev Testing Ctrl-C to stop collection of stats

Sample Time,instance #,cpu %,memory (MB),disk (MB),cell ip:port 2019-06-14T23:33:42-05:00, 0, 0.004659, 38.492188, 205.914062, 169.61.179.187:61440 2019-06-14T23:33:47-05:00, 0, 0.004659, 38.492188, 205.914062, 169.61.179.187:61440 2019-06-14T23:33:52-05:00, 0, 0.004659, 38.492188, 205.914062, 169.61.179.187:61440 2019-06-14T23:33:57-05:00, 0, 0.006533, 38.492188, 205.914062, 169.61.179.187:61440

I don't think the table is the issue, I think I'm just missing something ridiculously simple with how the data is being stored. I think the table can only pull display a string (or at least all the examples of I have found in our internal documentation only call strings).

  • 写回答

1条回答 默认 最新

  • douzhang7184 2019-06-15 15:56
    关注

    Solved using tabWriter thanks to Peter

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP