回答編集履歴
5
Colabの制約について追記
answer
CHANGED
@@ -44,4 +44,24 @@
|
|
44
44
|
}
|
45
45
|
}
|
46
46
|
}
|
47
|
-
```
|
47
|
+
```
|
48
|
+
|
49
|
+
## 追記:Colabの制約
|
50
|
+
|
51
|
+
どのみちColabでは「RAWモードな標準入力」は使えない様です。
|
52
|
+
conio.h相当を再現するライブラリを持ってしても結局のところはColabの実行画面が対応できません。
|
53
|
+
|
54
|
+
Colabの実行画面で標準入力を読もうとするとテキスト入力フォームが現れ、
|
55
|
+
「改行を契機」に標準入力に投げ込むことしかできません。
|
56
|
+
つまり、普通にos.Stdinを「改行を契機」に読むしかできません。
|
57
|
+
以下の様な使い方でどうぞ。
|
58
|
+
(この方法だともちろん文字入力ごとに何かをするということはできません。)
|
59
|
+
```go
|
60
|
+
s := bufio.NewScanner(os.Stdin)
|
61
|
+
s.Scan()
|
62
|
+
fmt.Println(s.Text())
|
63
|
+
```
|
64
|
+
|
65
|
+
そもそもColabは主にPython向けの環境なのでGoを試す場としては向かないし、
|
66
|
+
GoやCGOを動かすのもあまりColagサービスの想定とは
|
67
|
+
ズレていてコンパイルできたり動かしたりできたのはたまたまだと思います。
|
4
typo-fix&NewTimerよりNewTickerがお勧め。
answer
CHANGED
@@ -12,9 +12,9 @@
|
|
12
12
|
以下の様にサードパーティライブラリを使うことをお勧めします。
|
13
13
|
|
14
14
|
さらに、kbhit()相当は存在しないので、
|
15
|
-
「goroutineとchanでブロックする
|
15
|
+
「goroutineとchanでブロックするgetch.Getch()を非同期処理に変換」します。
|
16
16
|
以下の様に書くとtimerと並行してキー入力を受け付けることができます。
|
17
|
-
|
17
|
+
(おまけでNewTimerをなんども呼ぶよりはNewTickerを使うほうがリソースに優しいです。)
|
18
18
|
```go
|
19
19
|
package main
|
20
20
|
|
3
Getchを並行処理に置き換える方法を追記。
answer
CHANGED
@@ -10,13 +10,38 @@
|
|
10
10
|
|
11
11
|
Cの資産に詳しい方ならCGOでも良いと思うのですが、そうでないなら
|
12
12
|
以下の様にサードパーティライブラリを使うことをお勧めします。
|
13
|
+
|
14
|
+
さらに、kbhit()相当は存在しないので、
|
15
|
+
「goroutineとchanでブロックするgetchar.Getch()を非同期処理に変換」します。
|
16
|
+
以下の様に書くとtimerと並行してキー入力を受け付けることができます。
|
17
|
+
|
13
18
|
```go
|
14
19
|
package main
|
15
20
|
|
21
|
+
import (
|
22
|
+
"fmt"
|
16
|
-
|
23
|
+
"time"
|
17
24
|
|
25
|
+
"github.com/wzshiming/getch"
|
26
|
+
)
|
27
|
+
|
18
28
|
func main() {
|
29
|
+
input := make(chan string)
|
30
|
+
go func() {
|
31
|
+
for {
|
19
|
-
|
32
|
+
r, _, _ := getch.Getch()
|
20
|
-
|
33
|
+
input <- string(r)
|
34
|
+
}
|
35
|
+
}()
|
36
|
+
timer := time.NewTicker(1000 * time.Millisecond)
|
37
|
+
for {
|
38
|
+
select {
|
39
|
+
case <-timer.C:
|
40
|
+
fmt.Println("tick!")
|
41
|
+
case v := <-input:
|
42
|
+
fmt.Println("input:", v)
|
43
|
+
return
|
44
|
+
}
|
45
|
+
}
|
21
46
|
}
|
22
47
|
```
|
2
おすすめ追記
answer
CHANGED
@@ -4,4 +4,19 @@
|
|
4
4
|
|
5
5
|
以下の情報を参考にしてみてください。
|
6
6
|
|
7
|
-
[https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux](https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux)
|
7
|
+
[https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux](https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux)
|
8
|
+
|
9
|
+
## おすすめ追記
|
10
|
+
|
11
|
+
Cの資産に詳しい方ならCGOでも良いと思うのですが、そうでないなら
|
12
|
+
以下の様にサードパーティライブラリを使うことをお勧めします。
|
13
|
+
```go
|
14
|
+
package main
|
15
|
+
|
16
|
+
import "github.com/wzshiming/getch"
|
17
|
+
|
18
|
+
func main() {
|
19
|
+
r, b, err := getch.Getch()
|
20
|
+
println(r, b, err)
|
21
|
+
}
|
22
|
+
```
|
1
リンク修正
answer
CHANGED
@@ -4,4 +4,4 @@
|
|
4
4
|
|
5
5
|
以下の情報を参考にしてみてください。
|
6
6
|
|
7
|
-
https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux
|
7
|
+
[https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux](https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux)
|