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
+23k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+25.6k Golang : Get executable name behind process ID example
+5.9k Golang : Detect face in uploaded photo like GPlus
+15.6k Golang : Get file permission
+30.3k Golang : Interpolating or substituting variables in string examples
+15.4k Golang : Read large file with bufio.Scanner cause token too long error
+6k Golang : Break string into a slice of characters example
+15.6k Golang : How to reverse elements order in map ?
+6.3k Golang : Humanize and Titleize functions
+15.8k Golang : convert string or integer to big.Int type
+21.8k Golang : Repeat a character by multiple of x factor