Ply_py
Ply_py

Reputation: 115

How can I clear the console with golang in windows?

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

Upvotes: 8

Views: 12615

Answers (4)

Luca Marzi
Luca Marzi

Reputation: 806

If you look at the playground "Conway's Game of Life", you can see how they clear the terminal by means of a specific instruction:

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

Upvotes: 0

CommonSenseCode
CommonSenseCode

Reputation: 25369

For linux and mac in case someone needs it:

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

Upvotes: 4

Baba
Baba

Reputation: 95101

All you need is :

package main

import (
"os"
"os/exec"
)

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

Upvotes: 12

Intermernet
Intermernet

Reputation: 19388

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.

Upvotes: 11

Related Questions