mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 14:42:01 +00:00
good good study, day day up!
This commit is contained in:
51
vendor/gopl.io/ch8/countdown3/countdown.go
generated
vendored
Normal file
51
vendor/gopl.io/ch8/countdown3/countdown.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 246.
|
||||
|
||||
// Countdown implements the countdown for a rocket launch.
|
||||
package main
|
||||
|
||||
// NOTE: the ticker goroutine never terminates if the launch is aborted.
|
||||
// This is a "goroutine leak".
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
//!+
|
||||
|
||||
func main() {
|
||||
// ...create abort channel...
|
||||
|
||||
//!-
|
||||
|
||||
abort := make(chan struct{})
|
||||
go func() {
|
||||
os.Stdin.Read(make([]byte, 1)) // read a single byte
|
||||
abort <- struct{}{}
|
||||
}()
|
||||
|
||||
//!+
|
||||
fmt.Println("Commencing countdown. Press return to abort.")
|
||||
tick := time.Tick(1 * time.Second)
|
||||
for countdown := 10; countdown > 0; countdown-- {
|
||||
fmt.Println(countdown)
|
||||
select {
|
||||
case <-tick:
|
||||
// Do nothing.
|
||||
case <-abort:
|
||||
fmt.Println("Launch aborted!")
|
||||
return
|
||||
}
|
||||
}
|
||||
launch()
|
||||
}
|
||||
|
||||
//!-
|
||||
|
||||
func launch() {
|
||||
fmt.Println("Lift off!")
|
||||
}
|
Reference in New Issue
Block a user