首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Go中清除终端屏幕?

如何在Go中清除终端屏幕?
EN

Stack Overflow用户
提问于 2014-04-06 16:27:39
回答 4查看 38.8K关注 0票数 51

在Golang中有没有什么标准方法可以在我运行GO脚本时清除终端屏幕?或者我必须使用一些其他的库?

EN

回答 4

Stack Overflow用户

发布于 2014-04-06 17:28:56

您可以使用ANSI escape codes来完成此操作

fmt.Print("\033[H\033[2J")

但您应该知道,对于这类任务,没有防弹的跨平台解决方案。您应该检查平台(Windows / UNIX)并使用cls / clear或转义代码。

票数 49
EN

Stack Overflow用户

发布于 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)
    }
}
票数 16
EN

Stack Overflow用户

发布于 2014-04-06 17:04:12

正如所报告的here,您可以使用以下三行来清除屏幕:

c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()

不要忘记导入"os“和"os/exec”。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22891644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档