Golang : Overwrite previous output with count down timer
Just a note for myself on how to create a count down timer and how to overwrite previous output. Very useful in launching vehicles to space. ( I hope so ).
The code below will overwrite its own previous output and pay attention to the comment about play.golang.org and terminal.
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.Tick(time.Second)
fmt.Println("Counting down to launch...")
for i := 10; i >= 0; i-- {
<-ticker
fmt.Printf("\x0cOn 10/%d", i) // use \x0c for play.golang.org
//fmt.Printf("\rOn 10/%d", i) // use \r if you are running this in terminal
}
fmt.Println("\nWe have lift off!")
}
Output :
See http://play.golang.org/p/UAS4o5x2ce
Reference :
https://mmcgrana.github.io/2012/09/go-by-example-timers-and-tickers.html
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.3k Android Studio : Hello World example
+22.3k Golang : Calculate time different
+6.7k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+18.7k Golang : Delete item from slice based on index/key position
+23.5k Golang : Find biggest/largest number in array
+11.8k Golang : How to display image file or expose CSS, JS files from localhost?
+13.2k Golang : Check if an integer is negative or positive
+5.9k Golang : Selection sort example
+25.8k Golang : Calculate future date with time.Add() function
+34.6k Golang : Converting a negative number to positive number
+46.7k Golang : Convert int to byte array([]byte)
+4.5k Nginx and PageSpeed build from source CentOS example