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

前端 未结 4 817
粉色の甜心
粉色の甜心 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条回答
  •  感动是毒
    2021-01-30 04:27

    Use this solution if you want to rewrite multiple lines to the output. For instance, I made a decent Conway's "Game of Life" output using this method.

    DISCLAIMER: this only works on ANSI Terminals, and besides using fmt this isn't a Go-specific answer either.

    fmt.Printf("\033[0;0H")
    // put your other fmt.Printf(...) here
    

    Brief Explanation: this is an escape sequence which tells the ANSI terminal to move the cursor to a particular spot on the screen. The \033[ is the so-called escape sequence, and the 0;0H is the type of code telling the terminal move the cursor to row 0, column 0 of the terminal.

    Source: https://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements

提交回复
热议问题