| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
const ( |
| 9 |
period = 300 * time.Millisecond |
| 10 |
precision = 30 * time.Millisecond |
| 11 |
) |
| 12 |
|
| 13 |
func main() { |
| 14 |
counter := 0 |
| 15 |
p := time.Now() |
| 16 |
ticker := time.NewTicker(period) |
| 17 |
ch := make(chan int) |
| 18 |
|
| 19 |
go func() { |
| 20 |
for i := 0; i < 3; i++ { |
| 21 |
select { |
| 22 |
case t := <-ticker.C: |
| 23 |
counter = counter + 1 |
| 24 |
ch <- counter |
| 25 |
if d := t.Sub(p) - period; d < -precision || d > precision { |
| 26 |
fmt.Println("wrong delay", d) |
| 27 |
} |
| 28 |
p = t |
| 29 |
} |
| 30 |
} |
| 31 |
ch <- 0 |
| 32 |
}() |
| 33 |
for c := range ch { |
| 34 |
if c == 0 { |
| 35 |
break |
| 36 |
} |
| 37 |
println(c) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|