3 回答

TA貢獻1982條經(jīng)驗 獲得超2個贊
下面的 go 函數(shù)等價于 C 的 system() 函數(shù)。在 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
}
// 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
}

TA貢獻1850條經(jīng)驗 獲得超11個贊
您可以使用以下幾行來調用stdlib.h系統(tǒng)函數(shù):goC
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 關注
- 166 瀏覽
添加回答
舉報