系统(“清除”);在 Go 中等效

谁能告诉我转到系统中的等效项(“清除”);在 C 中?我想在一段时间后清洁控制台。提前致谢


编辑:这在 Linux 中对我有用


import "os/exec"


// Method body

clear := exec.Command("clear")

clear.Stdout = os.Stdout

clear.Run()


SMILET
浏览 150回答 3
3回答

临摹微笑

下面的 go 函数等价于 C 的 system() 函数。在 go 中,您可以像这样使用:import "system"...    exitstatus := system.System("clear")这是 Go 代码:package systemimport (        "os"        "os/exec"        "syscall"    )func System(cmd string) int {    c := exec.Command("sh", "-c", cmd)    c.Stdin = os.Stdin    c.Stdout = os.Stdout    c.Stderr = os.Stderr    err := c.Run()    if err == nil {        return 0    }    // Figure out the exit code    if ws, ok := c.ProcessState.Sys().(syscall.WaitStatus); ok {        if ws.Exited() {            return ws.ExitStatus()        }        if ws.Signaled() {            return -int(ws.Signal())        }    }    return -1}

慕盖茨4494581

您可以使用以下几行来调用stdlib.h系统函数:goCpackage main// #include <stdlib.h>//// void clear() {//&nbsp; system("clear");// }import "C"import (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; fmt.Println("Hello")&nbsp; &nbsp; fmt.Println("World")&nbsp; &nbsp; fmt.Println("Golang")&nbsp; &nbsp; time.Sleep(time.Second * 5)&nbsp; &nbsp; C.clear()&nbsp; &nbsp; fmt.Println("Screen is cleared")}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go