Skip to content

Commit 2a1ee90

Browse files
Using log instead of fmt
1 parent d7ee01b commit 2a1ee90

4 files changed

Lines changed: 18 additions & 17 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
package main
88

99
import (
10-
"fmt"
1110
"io"
11+
"log"
1212
"math/rand"
1313
"sync"
1414
"sync/atomic"
@@ -31,7 +31,7 @@ type dbConnection struct {
3131
// can be managed by the pool. Close performs any resource
3232
// release management.
3333
func (dbConn *dbConnection) Close() error {
34-
fmt.Println("Close: Connection", dbConn.ID)
34+
log.Println("Close: Connection", dbConn.ID)
3535
return nil
3636
}
3737

@@ -42,7 +42,7 @@ var idCounter int32
4242
// the pool when a new connection is needed.
4343
func createConnection() (io.Closer, error) {
4444
id := atomic.AddInt32(&idCounter, 1)
45-
fmt.Println("Create: New Connection", id)
45+
log.Println("Create: New Connection", id)
4646

4747
return &dbConnection{id}, nil
4848
}
@@ -55,7 +55,7 @@ func main() {
5555
// Create the pool to manage our connections.
5656
p, err := pool.New(createConnection, pooledResources)
5757
if err != nil {
58-
fmt.Println(err)
58+
log.Println(err)
5959
}
6060

6161
// Perform queries using connections from the pool.
@@ -73,7 +73,7 @@ func main() {
7373
wg.Wait()
7474

7575
// Close the pool.
76-
fmt.Println("Shutdown Program.")
76+
log.Println("Shutdown Program.")
7777
p.Close()
7878
}
7979

@@ -82,7 +82,7 @@ func performQueries(query int, p *pool.Pool) {
8282
// Acquire a connection from the pool.
8383
conn, err := p.Acquire()
8484
if err != nil {
85-
fmt.Println(err)
85+
log.Println(err)
8686
return
8787
}
8888

@@ -91,5 +91,5 @@ func performQueries(query int, p *pool.Pool) {
9191

9292
// Wait to simulate a query response.
9393
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
94-
fmt.Printf("Query: QID[%d] CID[%d]\n", query, conn.(*dbConnection).ID)
94+
log.Printf("Query: QID[%d] CID[%d]\n", query, conn.(*dbConnection).ID)
9595
}

‎chapter7/patterns/pool/pool.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package pool
66

77
import (
88
"errors"
9-
"fmt"
109
"io"
10+
"log"
1111
"sync"
1212
)
1313

@@ -44,15 +44,15 @@ func (p *Pool) Acquire() (io.Closer, error) {
4444
select {
4545
// Check for a free resource.
4646
case r, ok := <-p.resources:
47-
fmt.Println("Acquire:", "Shared Resource")
47+
log.Println("Acquire:", "Shared Resource")
4848
if !ok {
4949
return nil, ErrPoolClosed
5050
}
5151
return r, nil
5252

5353
// Provide a new resource since there are none available.
5454
default:
55-
fmt.Println("Acquire:", "New Resource")
55+
log.Println("Acquire:", "New Resource")
5656
return p.factory()
5757
}
5858
}
@@ -72,11 +72,11 @@ func (p *Pool) Release(r io.Closer) {
7272
select {
7373
// Attempt to place the new resource on the queue.
7474
case p.resources <- r:
75-
fmt.Println("Release:", "In Queue")
75+
log.Println("Release:", "In Queue")
7676

7777
// If the queue is already at cap we close the resource.
7878
default:
79-
fmt.Println("Release:", "Closing")
79+
log.Println("Release:", "Closing")
8080
r.Close()
8181
}
8282
}

‎chapter7/patterns/runner/main/main.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ func main() {
2424
// Create a new timer value for this run.
2525
r := runner.New(timeout)
2626

27-
// Add the tasks to be run and start running.
27+
// Add the tasks to be run.
2828
r.Add(createTask(1), createTask(2), createTask(1))
29-
err := r.Start()
30-
if err != nil {
29+
30+
// Run the tasks and handle the result.
31+
if err := r.Start(); err != nil {
3132
switch err {
3233
case runner.ErrTimeout:
3334
log.Println("Terminating Due To Timeout.")

‎chapter7/patterns/work/main/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
package main
88

99
import (
10-
"fmt"
10+
"log"
1111
"sync"
1212
"time"
1313

@@ -30,7 +30,7 @@ type namePrinter struct {
3030

3131
// Task implements the Worker interface.
3232
func (m *namePrinter) Task() {
33-
fmt.Println(m.name)
33+
log.Println(m.name)
3434
time.Sleep(time.Second)
3535
}
3636

0 commit comments

Comments
 (0)