shutdownlistener mac linea limpiar historial desinstalar consola common borrar go

go - mac - com.common.shutdownlistener desinstalar



¿Cómo puedo borrar la pantalla del terminal en Go? (5)

¿Hay algún método estándar en Golang para borrar la pantalla del terminal cuando ejecuto un script GO? o tengo que usar algunas otras bibliotecas?


Como se informa here , puede usar las siguientes tres líneas para borrar la pantalla:

c := exec.Command("clear") c.Stdout = os.Stdout c.Run()

No te olvides de importar "os" y "os / exec".


Fácil para sistemas nix:

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


Podrías hacerlo con códigos de escape ANSI :

print("/033[H/033[2J")

Pero debe saber que no existe una solución multiplataforma a prueba de balas para dicha tarea. Debe verificar la plataforma (Windows / UNIX) y usar cls / clear o códigos de escape.


Tienes que definir un método claro para cada sistema operativo diferente, como este. Cuando el sistema operativo del usuario no es compatible, entra en pánico.

package main import ( "fmt" "os" "os/exec" "runtime" "time" ) var clear map[string]func() //create a map for storing clear funcs func init() { clear = make(map[string]func()) //Initialize it clear["linux"] = func() { cmd := exec.Command("clear") //Linux example, its tested cmd.Stdout = os.Stdout cmd.Run() } clear["windows"] = func() { cmd := exec.Command("cmd", "/c", "cls") //Windows example, its tested cmd.Stdout = os.Stdout cmd.Run() } } func CallClear() { value, ok := clear[runtime.GOOS] //runtime.GOOS -> linux, windows, darwin etc. if ok { //if we defined a clear func for that platform: value() //we execute it } else { //unsupported platform panic("Your platform is unsupported! I can''t clear terminal screen :(") } } func main() { fmt.Println("I will clean the screen in 2 seconds!") time.Sleep(2 * time.Second) CallClear() fmt.Println("I''m alone...") }

(la ejecución del comando es de la respuesta de @merosss)


Usar goterm

package main import ( tm "github.com/buger/goterm" "time" ) func main() { tm.Clear() // Clear current screen for { // By moving cursor to top-left position we ensure that console output // will be overwritten each time, instead of adding new. tm.MoveCursor(1, 1) tm.Println("Current Time:", time.Now().Format(time.RFC1123)) tm.Flush() // Call it every time at the end of rendering time.Sleep(time.Second) } }