在Golang中有没有什么标准方法可以在我运行GO脚本时清除终端屏幕?或者我必须使用一些其他的库?
发布于 2014-04-06 17:28:56
您可以使用ANSI escape codes来完成此操作
fmt.Print("\033[H\033[2J")
但您应该知道,对于这类任务,没有防弹的跨平台解决方案。您应该检查平台(Windows / UNIX)并使用cls
/ clear
或转义代码。
发布于 2016-04-13 16:20:41
使用goterm
package main
import (
tm "github.com/buger/goterm"
"time"
)
func main() {
tm.Clear() // Clear current screen
for {
// By moving cursor to top-left position we ensure that console output
// will be overwritten each time, instead of adding new.
tm.MoveCursor(1, 1)
tm.Println("Current Time:", time.Now().Format(time.RFC1123))
tm.Flush() // Call it every time at the end of rendering
time.Sleep(time.Second)
}
}
发布于 2014-04-06 17:04:12
正如所报告的here,您可以使用以下三行来清除屏幕:
c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
不要忘记导入"os“和"os/exec”。
https://stackoverflow.com/questions/22891644
复制相似问题