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
+8k Golang : Check from web if Go application is running or not
+5.2k Golang : Issue HTTP commands to server and port example
+14k Golang : Google Drive API upload and rename example
+12.3k Golang : Encrypt and decrypt data with x509 crypto
+10.6k Golang : ISO8601 Duration Parser example
+9.8k Golang : Get current, epoch time and display by year, month and day
+29.2k Golang : missing Git command
+20.5k Golang : Pipe output from one os.Exec(shell command) to another command
+32.7k Golang : Regular Expression for alphanumeric and underscore
+16k Golang : How to reverse elements order in map ?
+10.8k PHP : Convert(cast) bigInt to string
+8.3k Golang : Check if integer is power of four example