How to have an in-place string that updates on stdout

前端 未结 4 753
粉色の甜心
粉色の甜心 2021-01-30 03:57

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

4条回答
  •  猫巷女王i
    2021-01-30 04:33

    The solution for one string which will replace whole string

    fmt.Printf("\033[2K\r%d", i)

    For example, it correctly prints from 10 to 0:

    for i:= 10; i>=0; i-- {
        fmt.Printf("\033[2K\r%d", i)
        time.Sleep(1 * time.Second)
    }
    fmt.Println()
    

    which previous answers don't solve.

提交回复
热议问题