简体   繁体   中英

How to clear last line in terminal with Golang

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM