英文:
system("clear"); equivalent in Go
问题
有人可以告诉我在Go语言中如何实现类似于C语言中的system("clear")的功能吗?我想在一定时间后清空控制台。提前谢谢!
编辑:这个方法在Linux中适用
import "os/exec"
// 方法体
clear := exec.Command("clear")
clear.Stdout = os.Stdout
clear.Run()
英文:
Could anyone show me the equivalent in go to the system("clear"); in C ? I want to clean the console after a certain period of time. Thanks in advance
EDIT: This worked for me in Linux
import "os/exec"
// Method body
clear := exec.Command("clear")
clear.Stdout = os.Stdout
clear.Run()
答案1
得分: 3
下面的go函数等同于C语言的system()函数。在go语言中,你可以这样使用:
import "system"
...
exitstatus := system.System("clear")
以下是Go代码:
package system
import (
    "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
    }
    // 确定退出码
    if ws, ok := c.ProcessState.Sys().(syscall.WaitStatus); ok {
        if ws.Exited() {
            return ws.ExitStatus()
        }
        if ws.Signaled() {
            return -int(ws.Signal())
        }
    }
    return -1
}
英文:
The go function below is equivalent to the C system() function. In go you could use this like:
<!-- language-all: go -->
import "system"
...
    exitstatus := system.System("clear")
Here is the Go code:
package system
import (
        "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
}
答案2
得分: 1
你可以使用几行C代码在Go语言中调用stdlib.h系统函数:
package main
// #include <stdlib.h>
//
// void clear() {
//  system("clear");
// }
import "C"
import (
	"fmt"
	"time"
)
func main() {
	fmt.Println("Hello")
	fmt.Println("World")
	fmt.Println("Golang")
	time.Sleep(time.Second * 5)
	C.clear()
	fmt.Println("Screen is cleared")
}
这段代码通过在Go语言中嵌入C代码来调用stdlib.h中的clear函数,该函数用于清除屏幕。在Go语言中,使用import "C"导入C代码,并在需要调用C函数的地方使用C.前缀。在这个例子中,我们在main函数中调用了C.clear()函数来清除屏幕。
英文:
You can call the stdlib.h system function in go by using few lines of C:
package main
// #include <stdlib.h>
//
// void clear() {
//  system("clear");
// }
import "C"
import (
	"fmt"
	"time"
)
func main() {
	fmt.Println("Hello")
	fmt.Println("World")
	fmt.Println("Golang")
	time.Sleep(time.Second * 5)
	C.clear()
	fmt.Println("Screen is cleared")
}
答案3
得分: 0
system()在C语言中调用外部程序,例如你的情况下是clear。
因此,在Go语言中,等效的方法是使用os/exec标准包来启动相同的程序,并等待其执行完成。
英文:
system() in C calls an external program — clear in your case.
So the equivalent in Go would be using the os/exec standard package to spawn the same program and wait for it to complete its execution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论