Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I clear the console with golang in windows?

Tags:

windows

go

I've tried a lot of ways, like

package main 
import (
    "os"
    "os/exec"
)

func main() {
    c := exec.Command("cls")
    c.Stdout = os.Stdout
    c.Run()
}

and

C.system(C.CString("cls"))

And the escape sequence doesn't work either

like image 276
Ply_py Avatar asked Oct 06 '13 13:10

Ply_py


3 Answers

There's really no easy way to do this in a cross-platform way using the standard libraries.

termbox-go seems to be one library providing cross-platform terminal control. There are probably others, but it's the only one I've used and it seems to work well.

Clearing the console using termbox-go would be a matter of doing a Clear and then a Flush.

See http://godoc.org/github.com/nsf/termbox-go for more details.

like image 155
Intermernet Avatar answered Sep 26 '22 00:09

Intermernet


All you need is :

package main

import (
"os"
"os/exec"
)

func main() {
    cmd := exec.Command("cmd", "/c", "cls")
    cmd.Stdout = os.Stdout
    cmd.Run()
}
like image 38
Baba Avatar answered Sep 27 '22 00:09

Baba


For linux and mac in case someone needs it:

fmt.Println("\033[2J")
like image 45
CommonSenseCode Avatar answered Sep 25 '22 00:09

CommonSenseCode