How to clear console using golang in windows?

I tried many ways, for example

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

+7
windows go
source share
4 answers

All you need is:

 package main import ( "os" "os/exec" ) func main() { cmd := exec.Command("cmd", "/c", "cls") cmd.Stdout = os.Stdout cmd.Run() } 
+9
source share

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

termbox-go seems to be one library that provides cross-platform terminal management. There may be others, but this is the only one I used and it seems to work well.

Removing the console using termbox-go would be a Clear question, and then Flush .

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

+11
source share

For Linux and Mac, if anyone needs it:

 fmt.Println("\033[2J") 
+1
source share

If you look at the Play Conway of Life playground, you can see how they clean the terminal using special instructions:

//line 110 fmt.Print("\x0c", l)

0
source share

All Articles