Skip to content

Clear screen (clear console) in Golang

There are two ways to clear screen/console in Golang:

with exec

package main

import (
    "os"
    "os/exec"
)

func main(){
    fmt.println("hello world")
    cmd := exec.Comand("clear")
    cmd.Stdout = os.Stdout
    cmd.Run()
}

with ANSI

fmt.Printf("\x1bc")
// from bottom

// OR

fmt.Printf("\x1b[2J")
// from top

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try my iOS App