Skip to content

Commit 64da0e0

Browse files
committed
Update async2 example
1 parent 67c1355 commit 64da0e0

File tree

6 files changed

+126
-111
lines changed

6 files changed

+126
-111
lines changed

examples/basic/helloworld/async2.go

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package async2
2+
3+
import (
4+
"log"
5+
"math/rand"
6+
"sync"
7+
"time"
8+
)
9+
10+
// Async2 -- HelloWorld 非同期版 (2)
11+
func Async2() error {
12+
log.SetFlags(0)
13+
14+
var (
15+
wg = new(sync.WaitGroup)
16+
ch = make(chan string)
17+
delay = func() time.Duration {
18+
return time.Duration(rand.Intn(200)) * time.Millisecond
19+
}
20+
)
21+
22+
var (
23+
hello = newRunner("hello", wg, ch, delay)
24+
world = newRunner("world", wg, ch, delay)
25+
closer = newCloser(wg, ch)
26+
printer = newPrinter(ch)
27+
)
28+
29+
wg.Add(2)
30+
31+
go hello.run()
32+
go world.run()
33+
go closer.run()
34+
35+
printer.run()
36+
37+
return nil
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package async2
2+
3+
import "sync"
4+
5+
type _closer struct {
6+
wg *sync.WaitGroup
7+
ch chan<- string
8+
}
9+
10+
func newCloser(wg *sync.WaitGroup, ch chan<- string) *_closer {
11+
c := new(_closer)
12+
13+
c.wg = wg
14+
c.ch = ch
15+
16+
return c
17+
}
18+
19+
func (me *_closer) run() {
20+
defer close(me.ch)
21+
me.wg.Wait()
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package async2
2+
3+
import "log"
4+
5+
type _printer struct {
6+
ch <-chan string
7+
}
8+
9+
func newPrinter(ch <-chan string) *_printer {
10+
p := new(_printer)
11+
p.ch = ch
12+
return p
13+
}
14+
15+
func (me *_printer) run() {
16+
for v := range me.ch {
17+
log.Println(v)
18+
}
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package async2
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
"time"
7+
)
8+
9+
const (
10+
LoopCount = 20
11+
)
12+
13+
type _runner struct {
14+
name string
15+
wg *sync.WaitGroup
16+
ch chan<- string
17+
delay func() time.Duration
18+
}
19+
20+
func newRunner(name string, wg *sync.WaitGroup, ch chan<- string, delay func() time.Duration) *_runner {
21+
r := new(_runner)
22+
23+
r.name = name
24+
r.wg = wg
25+
r.ch = ch
26+
r.delay = delay
27+
28+
return r
29+
}
30+
31+
func (me *_runner) String() string {
32+
return me.name
33+
}
34+
35+
func (me *_runner) run() {
36+
defer me.wg.Done()
37+
for i := 0; i < LoopCount; i++ {
38+
d := me.delay()
39+
time.Sleep(d)
40+
me.ch <- fmt.Sprintf("%d:%s (%v)", i, me, d)
41+
}
42+
}

examples/basic/helloworld/examples.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package helloworld
22

3-
import "github.com/devlights/try-golang/mapping"
3+
import (
4+
"github.com/devlights/try-golang/examples/basic/helloworld/async2"
5+
"github.com/devlights/try-golang/mapping"
6+
)
47

58
type (
69
register struct{}
@@ -15,6 +18,6 @@ func NewRegister() mapping.Register {
1518
func (r *register) Regist(m mapping.ExampleMapping) {
1619
m["helloworld_sync"] = Sync
1720
m["helloworld_async"] = Async
18-
m["helloworld_async2"] = Async2
21+
m["helloworld_async2"] = async2.Async2
1922
m["helloworld_mixed"] = Mixed
2023
}

0 commit comments

Comments
 (0)