How can I clear the console with golang in windows?

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.


All you need is :

package main

import (
"os"
"os/exec"
)

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

For linux and mac in case someone needs it:

fmt.Println("\033[2J")

Tags:

Windows

Go