Skip to content

Commit e70784f

Browse files
committed
[typ] fix cancel
1 parent 45fb69a commit e70784f

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

kadai3/imura81gt/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
【TRY】タイピングゲームを作ろう
2+
===========================================
3+
4+
- [ ] 標準出力に英単語を出す(出すものは自由)
5+
- [x] 標準入力から1行受け取る
6+
- [x] 制限時間内に何問解けたか表示する
7+
8+
9+
【TRY】分割ダウンローダを作ろう
10+
===========================================
11+
12+
分割ダウンロードを行う
13+
14+
- [ ]Rangeアクセスを用いる
15+
- [ ]いくつかのゴルーチンでダウンロードしてマージする
16+
- [ ]エラー処理を工夫する
17+
- [ ]golang.org/x/sync/errgourpパッケージなどを使ってみる
18+
- [ ]キャンセルが発生した場合の実装を行う
19+
20+
ref: https://qiita.com/codehex/items/d0a500ac387d39a34401
21+
22+
23+
Range Request
24+
-------------------------------------------------
25+
26+
https://developer.mozilla.org/ja/docs/Web/HTTP/Range_requests
27+
28+
> Accept-Ranges が HTTP レスポンスに存在した場合 (そして値が "none" ではない場合)、サーバーは範囲リクエストに対応しています。これは例えば、 HEAD リクエストを cURL で発行することで確認することができます。
29+
30+
31+
https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Accept-Ranges
32+
33+
> Accept-Ranges: bytes
34+
> Accept-Ranges: none
35+
36+
https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Range
37+
38+
> Range: <unit>=<range-start>-
39+
> Range: <unit>=<range-start>-<range-end>
40+
> Range: <unit>=<range-start>-<range-end>, <range-start>-<range-end>
41+
> Range: <unit>=<range-start>-<range-end>, <range-start>-<range-end>, <range-start>-<range-end>

kadai3/imura81gt/typ/typing/typing.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package typing
22

33
import (
44
"bufio"
5+
"context"
56
"fmt"
67
"io"
78
"math/rand"
@@ -35,8 +36,18 @@ func load() string {
3536
return fmt.Sprintf("%sも%sも%sのうち", w1, w2, w3)
3637
}
3738

39+
func show(score int, chars int, txt string) {
40+
fmt.Println(score, chars, ">", txt)
41+
fmt.Print(score, chars, " > ")
42+
}
43+
3844
// Run is a function to start typing-game.
3945
func Run() {
46+
47+
ctx := context.Background()
48+
ctx, cancel := context.WithCancel(ctx)
49+
defer cancel()
50+
4051
const (
4152
limit = 60
4253
interval = 10
@@ -48,24 +59,29 @@ func Run() {
4859

4960
txt := load()
5061

62+
show(score, chars, txt)
63+
5164
B:
5265
for {
53-
fmt.Println(score, chars, ">", txt)
54-
fmt.Print(score, chars, " > ")
5566
select {
5667
case v := <-chi:
5768
if txt == v {
5869
fmt.Println("GOOD!!!")
5970
score++
6071
chars = chars + len([]rune(txt))
6172
txt = load()
73+
show(score, chars, txt)
6274
} else {
6375
fmt.Println("BAD....")
76+
show(score, chars, txt)
6477
}
6578
case <-time.After(limit * time.Second):
6679
fmt.Println()
6780
fmt.Println("Time up!")
6881
fmt.Println("Score:", score, "points!", chars, "charactors!")
82+
cancel()
83+
case <-ctx.Done():
84+
fmt.Println(ctx.Err())
6985
break B
7086
}
7187
}

0 commit comments

Comments
 (0)