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
+9.2k SSL : The certificate is not trusted because no issuer chain was provided
+9.4k Golang : Send email with attachment(RFC2822) using Gmail API example
+3.5k Golang : If else example and common mistake
+9k Golang : Query string with space symbol %20 in between
+26.9k Golang : How to get HTTP request header information?
+18.2k Golang : Daemonizing a simple web server process example
+2.8k Linux : sudo yum updates not working
+5.9k Golang : Print how to use flag for your application example
+11.1k Golang : Get checkbox or extract multipart form data value example
+20.3k Golang : Upload to S3 with official aws-sdk-go package
+1.7k Java : Get FX sentiment from website example