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
+13.3k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+7.6k Golang : Get YouTube playlist
+20k Golang : Measure http.Get() execution time
+9.6k Golang : Get all countries currencies code in JSON format
+14.9k Golang : Get URI segments by number and assign as variable example
+10.8k Golang : Underscore string example
+7.3k Golang : Null and nil value
+9.4k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+8.8k Golang : Heap sort example
+7k Default cipher that OpenSSL used to encrypt a PEM file
+16k Golang : Update database with GORM example
+17.2k Golang : Covert map/slice/array to JSON or XML format