Skip to content

Commit 67c1355

Browse files
authored
Merge pull request #666 from devlights:add-async-helloworld2
Add examples/helloworld/async2 example
2 parents eee2d54 + e50e249 commit 67c1355

File tree

5 files changed

+118
-6
lines changed

5 files changed

+118
-6
lines changed

examples/advanced/zeromemorycopy/string2byteslice.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package zeromemorycopy
33
import (
44
"fmt"
55
"io"
6-
"reflect"
76
"strconv"
87
"strings"
98
"time"
@@ -52,8 +51,10 @@ func StringToByteSlice() error {
5251
// メモリコピー無しで変換
5352
// -------------------------------------
5453
elapsed = times.Stopwatch(func(start time.Time) {
55-
io.Discard.Write(unsafe.Slice((*byte)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)), len(s)))
54+
// reflect.StringHeader は、Go 1.20 で deprecated となった
55+
io.Discard.Write(unsafe.Slice(unsafe.StringData(s), len(s)))
5656

57+
//io.Discard.Write(unsafe.Slice((*byte)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)), len(s)))
5758
/* 上を細かく区切ると以下のようになる
5859
var (
5960
ptrStr = unsafe.Pointer(&s)

examples/basic/helloworld/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
|----|------------|----|
77
|sync.go|helloworld_sync|GO言語でのHelloWorldサンプル (同期版)|
88
|async.go|helloworld_async|GO言語でのHelloWorldサンプル (非同期版)|
9+
|async2.go|helloworld_async2|GO言語でのHelloWorldサンプル (非同期版)(2)|
910
|mixed.go|helloworld_mixed|同期と非同期の両方で同じことをするサンプル|
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package helloworld
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"math/rand"
7+
"sync"
8+
"time"
9+
)
10+
11+
const (
12+
LoopCount = 20
13+
)
14+
15+
type _runner struct {
16+
name string
17+
wg *sync.WaitGroup
18+
ch chan<- string
19+
delay func() time.Duration
20+
}
21+
22+
func newRunner(name string, wg *sync.WaitGroup, ch chan<- string, delay func() time.Duration) *_runner {
23+
r := new(_runner)
24+
25+
r.name = name
26+
r.wg = wg
27+
r.ch = ch
28+
r.delay = delay
29+
30+
return r
31+
}
32+
33+
func (me *_runner) String() string {
34+
return me.name
35+
}
36+
37+
func (me *_runner) run() {
38+
defer me.wg.Done()
39+
for i := 0; i < LoopCount; i++ {
40+
d := me.delay()
41+
time.Sleep(d)
42+
me.ch <- fmt.Sprintf("%d:%s (%v)", i, me, d)
43+
}
44+
}
45+
46+
type _closer struct {
47+
wg *sync.WaitGroup
48+
ch chan<- string
49+
}
50+
51+
func newCloser(wg *sync.WaitGroup, ch chan<- string) *_closer {
52+
c := new(_closer)
53+
54+
c.wg = wg
55+
c.ch = ch
56+
57+
return c
58+
}
59+
60+
func (me *_closer) run() {
61+
defer close(me.ch)
62+
me.wg.Wait()
63+
}
64+
65+
type _printer struct {
66+
ch <-chan string
67+
}
68+
69+
func newPrinter(ch <-chan string) *_printer {
70+
p := new(_printer)
71+
p.ch = ch
72+
return p
73+
}
74+
75+
func (me *_printer) run() {
76+
for v := range me.ch {
77+
log.Println(v)
78+
}
79+
}
80+
81+
// Async2 -- HelloWorld 非同期版 (2)
82+
func Async2() error {
83+
log.SetFlags(0)
84+
85+
var (
86+
wg = new(sync.WaitGroup)
87+
ch = make(chan string)
88+
delay = func() time.Duration {
89+
return time.Duration(rand.Intn(200)) * time.Millisecond
90+
}
91+
)
92+
93+
var (
94+
hello = newRunner("hello", wg, ch, delay)
95+
world = newRunner("world", wg, ch, delay)
96+
closer = newCloser(wg, ch)
97+
printer = newPrinter(ch)
98+
)
99+
100+
wg.Add(2)
101+
102+
go hello.run()
103+
go world.run()
104+
go closer.run()
105+
106+
printer.run()
107+
108+
return nil
109+
}

examples/basic/helloworld/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ func NewRegister() mapping.Register {
1515
func (r *register) Regist(m mapping.ExampleMapping) {
1616
m["helloworld_sync"] = Sync
1717
m["helloworld_async"] = Async
18+
m["helloworld_async2"] = Async2
1819
m["helloworld_mixed"] = Mixed
1920
}

examples/basic/helloworld/mixed.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func Mixed() error {
2525

2626
// start tasks
2727
var (
28-
syncCtx = sync(procCtx)
29-
asyncCtx = async(procCtx)
28+
syncCtx = syncOp(procCtx)
29+
asyncCtx = asyncOp(procCtx)
3030
)
3131

3232
// wait until all tasks are completed
@@ -35,7 +35,7 @@ func Mixed() error {
3535
return nil
3636
}
3737

38-
func sync(pCtx context.Context) context.Context {
38+
func syncOp(pCtx context.Context) context.Context {
3939
var (
4040
ctx, cxl = context.WithCancel(pCtx)
4141
)
@@ -52,7 +52,7 @@ func sync(pCtx context.Context) context.Context {
5252
return ctx
5353
}
5454

55-
func async(pCtx context.Context) context.Context {
55+
func asyncOp(pCtx context.Context) context.Context {
5656
var (
5757
ctx, cxl = context.WithCancel(pCtx)
5858
tasks = make([]context.Context, 0)

0 commit comments

Comments
 (0)