Skip to content

Instantly share code, notes, and snippets.

@peterkellydev
Last active April 18, 2016 22:04
Show Gist options
  • Save peterkellydev/e01e06a815c5a9373833fdd1bc179592 to your computer and use it in GitHub Desktop.
Save peterkellydev/e01e06a815c5a9373833fdd1bc179592 to your computer and use it in GitHub Desktop.
Rocket launch including abort!
package main
import (
"fmt"
"os"
"time"
)
// Demos multiplexing on channels - ticker for countdown and abort for interrupting (from stdin)
func main() {
abort := make(chan bool)
ticker := time.NewTicker(1 * time.Second)
go func() {
os.Stdin.Read(make([]byte, 1))
abort <- true
}()
fmt.Println("Initiating Countdown")
FINISH:
for i := 10; i >= 0; i-- {
select {
case <-ticker.C:
switch {
case i == 0:
fmt.Println("BLAST OFF!")
default:
fmt.Printf("%d...", i)
}
case <-abort:
fmt.Printf("Aborting launch!")
ticker.Stop()
break FINISH
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment