Skip to content

Commit e6a04d5

Browse files
author
gbbr
committed
Simplified closure
1 parent 022b258 commit e6a04d5

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

‎chapter7/patterns/pool/main/main.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ const (
1919
pooledResources = 2 // number of resources in the pool
2020
)
2121

22-
var counter int32
23-
2422
// dbConnection simulates a resource to share.
2523
type dbConnection struct {
2624
ID int32
@@ -32,6 +30,8 @@ func (dbConn *dbConnection) Close() {
3230
fmt.Println("Close: Connection", dbConn.ID)
3331
}
3432

33+
var counter int32
34+
3535
// createConnection is a factory method called by the pool
3636
// framework when new connections are needed.
3737
func createConnection() (pool.Resource, error) {
@@ -56,10 +56,10 @@ func main() {
5656
// Perform queries using a connection from the pool.
5757
for q := 0; q < maxGoroutines; q++ {
5858
// TODO: Explain about closures and why we are using params
59-
go func(q2 int, p2 pool.Interface) {
60-
performQueries(q2, p2)
59+
go func(n int) {
60+
performQueries(n, p)
6161
wg.Done()
62-
}(q, p)
62+
}(q)
6363

6464
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
6565
}

0 commit comments

Comments
 (0)