-
Notifications
You must be signed in to change notification settings - Fork 2
/
example4.go
96 lines (75 loc) · 1.99 KB
/
example4.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2014 Ardan Studios
//
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// This sample program demonstrates how to use a buffered
// channel to receive results from other goroutines in a guaranteed way.
package main
import (
"fmt"
"log"
"math/rand"
"time"
)
// result is what is sent back from each operation.
type result struct {
id int
op string
err error
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
// Set the number of routines and inserts.
const routines = 10
const inserts = routines * 2
// Buffered channel to receive information about any possible insert.
ch := make(chan result, inserts)
// Number of responses we need to handle.
waitInserts := inserts
// Perform all the inserts.
for i := 0; i < routines; i++ {
go func(id int) {
ch <- insertUser(id)
// We don't need to wait to start the second insert
// thanks to the buffered channel. The first send
// will happen immediately.
ch <- insertTrans(id)
}(i)
}
// Process the insert results as they complete.
for waitInserts > 0 {
// Wait for a response from a goroutine.
r := <-ch
// Display the result.
log.Printf("N: %d ID: %d OP: %s ERR: %v", waitInserts, r.id, r.op, r.err)
// Decrement the wait count and determine if we are done.
waitInserts--
}
log.Println("Inserts Complete")
}
// insertUser simulates a database operation.
func insertUser(id int) result {
r := result{
id: id,
op: fmt.Sprintf("insert USERS value (%d)", id),
}
// Randomize if the insert fails or not.
if rand.Intn(10) == 0 {
r.err = fmt.Errorf("Unable to insert %d into USER table", id)
}
return r
}
// insertTrans simulates a database operation.
func insertTrans(id int) result {
r := result{
id: id,
op: fmt.Sprintf("insert TRANS value (%d)", id),
}
// Randomize if the insert fails or not.
if rand.Intn(10) == 0 {
r.err = fmt.Errorf("Unable to insert %d into USER table", id)
}
return r
}