]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: handle os.Error values
authorRuss Cox <rsc@golang.org>
Fri, 28 Oct 2011 04:20:44 +0000 (21:20 -0700)
committerRuss Cox <rsc@golang.org>
Fri, 28 Oct 2011 04:20:44 +0000 (21:20 -0700)
Handling os.Error is no different than handling fmt.Stringer
here, so the code is redundant now, but it will be necessary
once error goes in.

Adding it now will make gofix fix it.

R=r
CC=golang-dev
https://golang.org/cl/5331045

src/pkg/fmt/doc.go
src/pkg/fmt/print.go

index c993e57a40a29bd2d75c820da2cbc93bffa32492..6713f0a16ed27c8e0495263d1797230068ff36fa 100644 (file)
        If an operand implements interface Formatter, that interface
        can be used for fine control of formatting.
 
-       If an operand implements method String() string that method
+       Next, if an operand implements the error interface, the Error method
        will be used to convert the object to a string, which will then
-       be formatted as required by the verb (if any). To avoid
-       recursion in cases such as
+       be formatted as required by the verb (if any).
+
+       Finally, if an operand implements method String() string that method
+       will be used to convert the object to a string, which will then
+       be formatted as required by the verb (if any).
+       To avoid recursion in cases such as
                type X int
                func (x X) String() string { return Sprintf("%d", x) }
        cast the value before recurring:
index f80ce7c9274851f2aa418a0e3b17dfaf617a4227..5e0237f4544e903add42486656082007e7538e9b 100644 (file)
@@ -630,12 +630,23 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString
                        return
                }
        } else {
-               // Is it a Stringer?
-               if stringer, ok := p.field.(Stringer); ok {
+               // Is it an error or Stringer?
+               // The duplication in the bodies is necessary:
+               // setting wasString and handled and deferring catchPanic
+               // must happen before calling the method.
+               switch v := p.field.(type) {
+               case os.Error:
                        wasString = false
                        handled = true
                        defer p.catchPanic(p.field, verb)
-                       p.printField(stringer.String(), verb, plus, false, depth)
+                       p.printField(v.String(), verb, plus, false, depth)
+                       return
+
+               case Stringer:
+                       wasString = false
+                       handled = true
+                       defer p.catchPanic(p.field, verb)
+                       p.printField(v.String(), verb, plus, false, depth)
                        return
                }
        }