Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear last line in terminal with Golang

Tags:

linux

terminal

go

I'm building CLI app (on Linux) where user has to choose something and then I have to clear last line.

I tried several things:

  fmt.Print("\r")
  fmt.Print("\033[1")

And the cursor goes back to beginning of line, but do not clear the text from the last line.

What am I missing here?

like image 247
pesho hristov Avatar asked Oct 22 '25 04:10

pesho hristov


1 Answers

You can use it that way

fmt.Printf("\033[1A\033[K")

\033[1A - one line up
\033[K - delete the line

For example

fmt.Println("hello")
fmt.Println("world")
fmt.Printf("\033[1A\033[K")

will output only hello as the world will be deleted

You can read more about ascii escaping here https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

like image 93
Daniel Taub Avatar answered Oct 23 '25 18:10

Daniel Taub