Last active
April 18, 2016 22:04
-
-
Save peterkellydev/e01e06a815c5a9373833fdd1bc179592 to your computer and use it in GitHub Desktop.
Rocket launch including abort!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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