October 2015
Beginner to intermediate
400 pages
14h 44m
English
select
The program below does the countdown for a rocket launch.
The time.Tick function returns a channel on which it sends
events periodically, acting like a metronome.
The value of each event is a timestamp, but it is rarely as
interesting as the fact of its delivery.
func main() {
fmt.Println("Commencing countdown.")
tick := time.Tick(1 * time.Second)
for countdown := 10; countdown > 0; countdown-- {
fmt.Println(countdown)
<-tick
}
launch()
}
Now let’s add the ability to abort the launch sequence by pressing the return key during the countdown. First, we start a goroutine that tries to read a single byte from the standard input and, if it succeeds, sends a value ...