我正在迭代一个数组并将每个数组元素的格式化字符串打印到终端(stdout)。我不想在新行上打印每个元素,而是想用程序的最新输出覆盖以前的输出。
我正在使用 macOS。
我尝试了几种方法:
// 'f' is the current element of the array
b := bytes.NewBufferString("")
if err != nil {
fmt.Printf("\rCould not retrieve file info for %s\n", f)
b.Reset()
} else {
fmt.Printf("\rRetrieved %s\n", f)
b.Reset()
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是\r
从字符串中删除并在每个输出之前添加附加 Printf:fmt.Printf("\033[0;0H")
。