Skip to content

Commit

Permalink
add Printf function
Browse files Browse the repository at this point in the history
  • Loading branch information
spyzhov committed Jun 22, 2020
1 parent a1a99b7 commit 49086bf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions close.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package safe

import (
"io"
"log"
)

// Close will close any io.Closer object if it's not nil, any error will be only logged.
func Close(closer io.Closer, scope string) {
if !IsNil(closer) {
if err := closer.Close(); !IsNil(err) {
log.Printf("error on close '%s': %s", scope, err)
Printf("error on close '%s': %s", scope, err)
}
}
}
4 changes: 4 additions & 0 deletions close_example_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package safe

import (
"fmt"
"net/http"
)

func ExampleClose() {
Printf = func(format string, v ...interface{}) {
fmt.Printf(format, v)
}
resp, err := http.DefaultClient.Get("http://example.com")
if err != nil {
panic(err)
Expand Down
1 change: 1 addition & 0 deletions close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type stub struct {
}

func TestClose(t *testing.T) {
Printf = func(format string, v ...interface{}) {}
tests := []struct {
name string
closer func() io.Closer
Expand Down
6 changes: 6 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package safe

import "log"

// Printf is the global function, will be called if Close method returns an error.
var Printf = log.Printf

0 comments on commit 49086bf

Please sign in to comment.