mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 17:36:12 +08:00
update book code
This commit is contained in:
42
eBook/examples/chapter_14/timer_goroutine.go
Normal file
42
eBook/examples/chapter_14/timer_goroutine.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// default.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
tick := time.Tick(1e8)
|
||||
boom := time.After(5e8)
|
||||
for {
|
||||
select {
|
||||
case <-tick:
|
||||
fmt.Println("tick.")
|
||||
case <-boom:
|
||||
fmt.Println("BOOM!")
|
||||
return
|
||||
default:
|
||||
fmt.Println(" .")
|
||||
time.Sleep(5e7)
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
.
|
||||
.
|
||||
tick.
|
||||
.
|
||||
.
|
||||
tick.
|
||||
.
|
||||
.
|
||||
tick.
|
||||
.
|
||||
.
|
||||
tick.
|
||||
.
|
||||
.
|
||||
tick.
|
||||
BOOM!
|
||||
*/
|
||||
Reference in New Issue
Block a user