1
0
mirror of https://github.com/golang/go synced 2024-11-22 02:14:40 -07:00

Effective Go: update examples to use new logging interface.

R=adg
CC=golang-dev
https://golang.org/cl/2468041
This commit is contained in:
Rob Pike 2010-10-12 16:56:50 -07:00
parent 96868c70ea
commit e787f8276d

View File

@ -463,7 +463,7 @@ statement, it's common to see one used to set up a local variable.
<pre> <pre>
if err := file.Chmod(0664); err != nil { if err := file.Chmod(0664); err != nil {
log.Stderr(err) log.Print(err)
return err return err
} }
</pre> </pre>
@ -815,7 +815,7 @@ which is much clearer than placing it at the end of the function.
</p> </p>
<p> <p>
The arguments to the deferred function (which includes the receiver if The arguments to the deferred function (which include the receiver if
the function is a method) are evaluated when the <i>defer</i> the function is a method) are evaluated when the <i>defer</i>
executes, not when the <i>call</i> executes. Besides avoiding worries executes, not when the <i>call</i> executes. Besides avoiding worries
about variables changing values as the function executes, this means about variables changing values as the function executes, this means
@ -1288,7 +1288,7 @@ func offset(tz string) int {
if seconds, ok := timeZone[tz]; ok { if seconds, ok := timeZone[tz]; ok {
return seconds return seconds
} }
log.Stderr("unknown time zone", tz) log.Println("unknown time zone", tz)
return 0 return 0
} }
</pre> </pre>
@ -1455,13 +1455,13 @@ Within the function <code>Printf</code>, <code>v</code> acts like a variable of
<code>[]interface{}</code> but if it is passed to another variadic function, it acts like <code>[]interface{}</code> but if it is passed to another variadic function, it acts like
a regular list of arguments. a regular list of arguments.
Here is the implementation of the Here is the implementation of the
function <code>log.Stderr</code> we used above. It passes its arguments directly to function <code>log.Println</code> we used above. It passes its arguments directly to
<code>fmt.Sprintln</code> for the actual formatting. <code>fmt.Sprintln</code> for the actual formatting.
</p> </p>
<pre> <pre>
// Stderr is a helper function for easy logging to stderr. It is analogous to Fprintln(os.Stderr). // Println prints to the standard logger in the manner of fmt.Println.
func Stderr(v ...interface{}) { func Println(v ...interface{}) {
stderr.Output(2, fmt.Sprintln(v)) // Output takes parameters (int, string) std.Output(2, fmt.Sprintln(v...)) // Output takes parameters (int, string)
} }
</pre> </pre>
<p> <p>
@ -2133,7 +2133,7 @@ func NewJob(command string, logger *log.Logger) *Job {
or with a composite literal, or with a composite literal,
</p> </p>
<pre> <pre>
job := &amp;Job{command, log.New(os.Stderr, nil, "Job: ", log.Ldate)} job := &amp;Job{command, log.New(os.Stderr, "Job: ", log.Ldate)}
</pre> </pre>
<p> <p>
If we need to refer to an embedded field directly, the type name of the field, If we need to refer to an embedded field directly, the type name of the field,
@ -2675,7 +2675,7 @@ func server(workChan <-chan *Work) {
func safelyDo(work *Work) { func safelyDo(work *Work) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.Stderr("work failed:", err) log.Println("work failed:", err)
} }
}() }()
do(work) do(work)