小编sgm*_*sgm的帖子

如何使用 Go 将格式化字符串打印到 stdout 中的同一行?

我正在迭代一个数组并将每个数组元素的格式化字符串打印到终端(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")

string macos stdout go

6
推荐指数
1
解决办法
9246
查看次数

标签 统计

go ×1

macos ×1

stdout ×1

string ×1