mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-09 16:42:26 +00:00
ch8: fix code format
This commit is contained in:
@@ -2,27 +2,27 @@
|
||||
|
||||
下面的程序會進行火箭發射的倒計時。time.Tick函數返迴一個channel,程序會週期性地像一個節拍器一樣向這個channel發送事件。每一個事件的值是一個時間戳,不過更有意思的是其傳送方式。
|
||||
|
||||
<u><i>gopl.io/ch8/countdown1</i></u>
|
||||
```go
|
||||
gopl.io/ch8/countdown1
|
||||
func main() {
|
||||
fmt.Println("Commencing countdown.")
|
||||
tick := time.Tick(1 * time.Second)
|
||||
for countdown := 10; countdown > 0; countdown-- {
|
||||
fmt.Println(countdown)
|
||||
j<-tick
|
||||
}
|
||||
launch()
|
||||
fmt.Println("Commencing countdown.")
|
||||
tick := time.Tick(1 * time.Second)
|
||||
for countdown := 10; countdown > 0; countdown-- {
|
||||
fmt.Println(countdown)
|
||||
j<-tick
|
||||
}
|
||||
launch()
|
||||
}
|
||||
```
|
||||
|
||||
現在我們讓這個程序支持在倒計時中,用戶按下return鍵時直接中斷發射流程。首先,我們啟動一個goroutine,這個goroutine會嚐試從標準輸入中調入一個單獨的byte併且,如果成功了,會向名爲abort的channel發送一個值。
|
||||
|
||||
<u><i>gopl.io/ch8/countdown2</i></u>
|
||||
```go
|
||||
gopl.io/ch8/countdown2
|
||||
abort := make(chan struct{})
|
||||
go func() {
|
||||
os.Stdin.Read(make([]byte, 1)) // read a single byte
|
||||
abort <- struct{}{}
|
||||
os.Stdin.Read(make([]byte, 1)) // read a single byte
|
||||
abort <- struct{}{}
|
||||
}()
|
||||
```
|
||||
|
||||
@@ -31,13 +31,13 @@ go func() {
|
||||
```go
|
||||
select {
|
||||
case <-ch1:
|
||||
// ...
|
||||
// ...
|
||||
case x := <-ch2:
|
||||
// ...use x...
|
||||
// ...use x...
|
||||
case ch3 <- y:
|
||||
// ...
|
||||
// ...
|
||||
default:
|
||||
// ...
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
@@ -47,20 +47,19 @@ select會等待case中有能夠執行的case時去執行。當條件滿足時,
|
||||
|
||||
讓我們迴到我們的火箭發射程序。time.After函數會立卽返迴一個channel,併起一個新的goroutine在經過特定的時間後向該channel發送一個獨立的值。下面的select語句會會一直等待到兩個事件中的一個到達,無論是abort事件或者一個10秒經過的事件。如果10秒經過了還沒有abort事件進入,那麽火箭就會發射。
|
||||
|
||||
|
||||
```go
|
||||
func main() {
|
||||
// ...create abort channel...
|
||||
// ...create abort channel...
|
||||
|
||||
fmt.Println("Commencing countdown. Press return to abort.")
|
||||
select {
|
||||
case <-time.After(10 * time.Second):
|
||||
// Do nothing.
|
||||
case <-abort:
|
||||
fmt.Println("Launch aborted!")
|
||||
return
|
||||
}
|
||||
launch()
|
||||
fmt.Println("Commencing countdown. Press return to abort.")
|
||||
select {
|
||||
case <-time.After(10 * time.Second):
|
||||
// Do nothing.
|
||||
case <-abort:
|
||||
fmt.Println("Launch aborted!")
|
||||
return
|
||||
}
|
||||
launch()
|
||||
}
|
||||
```
|
||||
|
||||
@@ -70,11 +69,11 @@ func main() {
|
||||
```go
|
||||
ch := make(chan int, 1)
|
||||
for i := 0; i < 10; i++ {
|
||||
select {
|
||||
case x := <-ch:
|
||||
fmt.Println(x) // "0" "2" "4" "6" "8"
|
||||
case ch <- i:
|
||||
}
|
||||
select {
|
||||
case x := <-ch:
|
||||
fmt.Println(x) // "0" "2" "4" "6" "8"
|
||||
case ch <- i:
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -82,24 +81,24 @@ for i := 0; i < 10; i++ {
|
||||
|
||||
下面讓我們的發射程序打印倒計時。這里的select語句會使每次循環迭代等待一秒來執行退出操作。
|
||||
|
||||
<u><i>gopl.io/ch8/countdown3</i></u>
|
||||
```go
|
||||
gopl.io/ch8/countdown3
|
||||
func main() {
|
||||
// ...create abort channel...
|
||||
// ...create abort channel...
|
||||
|
||||
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()
|
||||
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()
|
||||
}
|
||||
```
|
||||
|
||||
@@ -120,10 +119,10 @@ ticker.Stop() // cause the ticker's goroutine to terminate
|
||||
```go
|
||||
select {
|
||||
case <-abort:
|
||||
fmt.Printf("Launch aborted!\n")
|
||||
return
|
||||
fmt.Printf("Launch aborted!\n")
|
||||
return
|
||||
default:
|
||||
// do nothing
|
||||
// do nothing
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user