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.2k Golang : How to get a user home directory path?
+30.4k Golang : How to redirect to new page with net/http?
+34k Golang : Call a function after some delay(time.Sleep and Tick)
+51.5k Golang : Check if item is in slice/array
+8.4k Golang : Convert word to its plural form example
+19.3k Golang : Check if directory exist and create if does not exist
+7.8k Golang : How to execute code at certain day, hour and minute?
+16.5k Golang : Test floating point numbers not-a-number and infinite example
+30k Golang : How to get HTTP request header information?
+31k Golang : Interpolating or substituting variables in string examples
+11.4k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+12.7k Golang : Get absolute path to binary for os.Exec function with exec.LookPath