File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -18,4 +18,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
1818 m ["loops_channel_loop" ] = ChannelLoop
1919 m ["loops_map_loop" ] = MapLoop
2020 m ["loops_range_loop" ] = RangeLoop
21+ m ["loops_infinite_loop" ] = InfiniteLoop
2122}
Original file line number Diff line number Diff line change 1+ package loops
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "time"
7+ )
8+
9+ // InfiniteLoop は、無限ループのサンプルです.
10+ func InfiniteLoop () error {
11+ var (
12+ mainCtx , mainCxl = context .WithCancel (context .Background ())
13+ procCtx , procCxl = context .WithTimeout (mainCtx , 3 * time .Second )
14+ )
15+
16+ defer mainCxl ()
17+ defer procCxl ()
18+
19+ // Go では 無限ループ は以下のように for {} とだけ書く
20+ LOOP:
21+ for {
22+ select {
23+ case <- time .After (200 * time .Millisecond ):
24+ fmt .Print ("." )
25+ case <- procCtx .Done ():
26+ break LOOP
27+ }
28+ }
29+
30+ fmt .Println ("" )
31+
32+ return nil
33+ }
You can’t perform that action at this time.
0 commit comments