From: James Fysh Date: Fri, 27 Aug 2010 21:54:16 +0000 (+1000) Subject: Documentation: Fix a bug in the example in Constants subsection X-Git-Tag: weekly.2010-09-06~56 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=089ce170930ebdf0908369fa7d0a30647488f2af;p=gostls13.git Documentation: Fix a bug in the example in Constants subsection Not a bug per-se, the issue is that the fmt.Sprintf method inside the ByteSize.String() method ends up calling itself to generate the String representation of the ByteSize value. Infinite loops are bad. Updated as per review comments R=r CC=golang-dev https://golang.org/cl/1974046 --- diff --git a/doc/effective_go.html b/doc/effective_go.html index 8083e9fbc9..a04152e49c 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -1539,26 +1539,29 @@ automatically for printing, even as part of a general type. func (b ByteSize) String() string { switch { case b >= YB: - return fmt.Sprintf("%.2fYB", b/YB) + return fmt.Sprintf("%.2fYB", float64(b/YB)) case b >= ZB: - return fmt.Sprintf("%.2fZB", b/ZB) + return fmt.Sprintf("%.2fZB", float64(b/ZB)) case b >= EB: - return fmt.Sprintf("%.2fEB", b/EB) + return fmt.Sprintf("%.2fEB", float64(b/EB)) case b >= PB: - return fmt.Sprintf("%.2fPB", b/PB) + return fmt.Sprintf("%.2fPB", float64(b/PB)) case b >= TB: - return fmt.Sprintf("%.2fTB", b/TB) + return fmt.Sprintf("%.2fTB", float64(b/TB)) case b >= GB: - return fmt.Sprintf("%.2fGB", b/GB) + return fmt.Sprintf("%.2fGB", float64(b/GB)) case b >= MB: - return fmt.Sprintf("%.2fMB", b/MB) + return fmt.Sprintf("%.2fMB", float64(b/MB)) case b >= KB: - return fmt.Sprintf("%.2fKB", b/KB) + return fmt.Sprintf("%.2fKB", float64(b/KB)) } - return fmt.Sprintf("%.2fB", b) + return fmt.Sprintf("%.2fB", float64(b)) }

+(The float64 conversions prevent Sprintf +from recurring back through the String method for +ByteSize.) The expression YB prints as 1.00YB, while ByteSize(1e13) prints as 9.09TB.