-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.go
More file actions
64 lines (56 loc) · 1.09 KB
/
generator.go
File metadata and controls
64 lines (56 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// generator.go imitates python's generator, get the value with Next method.
package goTools
type GenerateI interface {
Generate()
Next() (int, bool)
Reset()
Close()
}
// Generator 定义生成器类型
type Generator struct {
start int
end int
step int
current int
ch chan int
control chan struct{}
}
// NewGenerator 创建一个新的生成器实例
func NewGenerator(start, end, step int) *Generator {
return &Generator{
start: start,
end: end,
step: step,
current: start,
ch: make(chan int),
control: make(chan struct{}),
}
}
// Generate 启动生成过程
func (g *Generator) Generate() {
go func() {
for g.current < g.end {
select {
case g.ch <- g.current:
g.current += g.step
case <-g.control:
return
}
}
close(g.ch)
}()
}
// Next 获取下一个值
func (g *Generator) Next() (int, bool) {
val, open := <-g.ch
return val, open
}
// Reset 重置生成器状态
func (g *Generator) Reset() {
g.current = g.start
g.ch = make(chan int)
}
// Close 停止生成过程
func (g *Generator) Close() {
g.control <- struct{}{}
}