dongwei9365 2013-03-15 21:21
浏览 75
已采纳

如何拥有在标准输出上更新的就地字符串

I want to output to stdout and have the output "overwrite" the previous output.

For example; if I output On 1/10, I want the next output On 2/10 to overwrite On 1/10. How can I do this?

  • 写回答

3条回答 默认 最新

  • drmlxgmqn18198265 2013-03-15 21:54
    关注

    stdout is a stream (io.Writer). You cannot modify what was already written to it. What can be changed is how that stream's represented in case it is printed to a terminal. Note that there's no good reason to assume this scenario. For example, a user could redirect stdout to a pipe or to a file at will.

    So the proper approach is to first check:

    • if the stdout is going to a terminal
    • what is that terminal's procedure to overwrite a line/screen

    Both of the above are out of this question's scope, but let's assume that a terminal is our device. Then usually, printing:

    fmt.Printf("On %d/10", i)
    

    will overwrite the previous line in the terminal. stands for carriage return, implemented by many terminals as moving the cursor to the beginning of the current line, hence providing the "overwrite line" facility.

    As an example of "other" terminal with a differently supported 'overwriting', here is an example at the playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)