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
+4.8k Linux : sudo yum updates not working
+27.8k PHP : Convert(cast) string to bigInt
+33.8k Golang : How to check if slice or array is empty?
+5.8k Golang : Struct field tags and what is their purpose?
+19.4k Golang : Display list of time zones with GMT
+28.8k Get file path of temporary file in Go
+6.3k Golang : Get missing location after unmarshal binary and gob decode time.
+14.3k Elastic Search : Mapping date format and sort by date
+19.6k Golang : Example for DSA(Digital Signature Algorithm) package functions
+7.4k Golang : Of hash table and hash map
+20.4k Golang : Compare floating-point numbers