Commit 5353e1ef authored by Andrew Gerrand's avatar Andrew Gerrand

doc: trim spaces from code snippets

gofmt likes to put lines like
  // STOP OMIT
two blank lines from a closing brace, creating an ugly space inside
<pre> blocks in some of these files. This change resolves this issue.

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/5520044
parent 5f5a7eb4
......@@ -37,8 +37,7 @@ contents of one file to the other:
dst.Close()
src.Close()
return
}
</pre>
}</pre>
<p>
This works, but there is a bug. If the second call to os.Open fails, the
......@@ -64,8 +63,7 @@ files are always closed:
defer dst.Close()
return io.Copy(dst, src)
}
</pre>
}</pre>
<p>
Defer statements allow us to think about closing each file right after opening
......@@ -94,8 +92,7 @@ deferred. The deferred call will print "0" after the function returns.
defer fmt.Println(i)
i++
return
}
</pre>
}</pre>
<p>
2. <i>Deferred function calls are executed in Last In First Out order
......@@ -111,8 +108,7 @@ This function prints "3210":
for i := 0; i &lt; 4; i++ {
defer fmt.Print(i)
}
}
</pre>
}</pre>
<p>
3. <i>Deferred functions may read and assign to the returning function's named
......@@ -128,8 +124,7 @@ the surrounding function returns. Thus, this function returns 2:
-->func c() (i int) {
defer func() { i++ }()
return 1
}
</pre>
}</pre>
<p>
This is convenient for modifying the error return value of a function; we will
......@@ -188,8 +183,7 @@ func g(i int) {
defer fmt.Println(&#34;Defer in g&#34;, i)
fmt.Println(&#34;Printing in g&#34;, i)
g(i + 1)
}
</pre>
}</pre>
<p>
The function g takes the int i, and panics if i is greater than 3, or else it
......
......@@ -12,8 +12,7 @@ returns a non-nil <code>error</code> value when it fails to open a file.
</p>
<pre><!--{{code "progs/error.go" `/func Open/`}}
-->func Open(name string) (file *File, err error)
</pre>
-->func Open(name string) (file *File, err error)</pre>
<p>
The following code uses <code>os.Open</code> to open a file. If an error
......@@ -21,12 +20,11 @@ occurs it calls <code>log.Fatal</code> to print the error message and stop.
</p>
<pre><!--{{code "progs/error.go" `/func openFile/` `/STOP/`}}
--> f, err := os.Open(&#34;filename.ext&#34;)
-->f, err := os.Open(&#34;filename.ext&#34;)
if err != nil {
log.Fatal(err)
}
// do something with the open *File f
</pre>
// do something with the open *File f</pre>
<p>
You can get a lot done in Go knowing just this about the <code>error</code>
......@@ -67,8 +65,7 @@ type errorString struct {
func (e *errorString) Error() string {
return e.s
}
</pre>
}</pre>
<p>
You can construct one of these values with the <code>errors.New</code>
......@@ -80,8 +77,7 @@ and returns as an <code>error</code> value.
-->// New returns an error that formats as the given text.
func New(text string) error {
return &amp;errorString{text}
}
</pre>
}</pre>
<p>
Here's how you might use <code>errors.New</code>:
......@@ -93,8 +89,7 @@ Here's how you might use <code>errors.New</code>:
return 0, errors.New(&#34;math: square root of negative number&#34;)
}
// implementation
}
</pre>
}</pre>
<p>
A caller passing a negative argument to <code>Sqrt</code> receives a non-nil
......@@ -105,11 +100,10 @@ A caller passing a negative argument to <code>Sqrt</code> receives a non-nil
</p>
<pre><!--{{code "progs/error.go" `/func printErr/` `/STOP/`}}
--> f, err := Sqrt(-1)
-->f, err := Sqrt(-1)
if err != nil {
fmt.Println(err)
}
</pre>
}</pre>
<p>
The <a href="/pkg/fmt/">fmt</a> package formats an <code>error</code> value
......@@ -131,10 +125,9 @@ rules and returns it as an <code>error</code> created by
</p>
<pre><!--{{code "progs/error.go" `/fmtError/` `/STOP/`}}
--> if f &lt; 0 {
-->if f &lt; 0 {
return 0, fmt.Errorf(&#34;math: square root of negative number %g&#34;, f)
}
</pre>
}</pre>
<p>
In many cases <code>fmt.Errorf</code> is good enough, but since
......@@ -153,8 +146,7 @@ error implementation instead of using <code>errors.errorString</code>:
func (f NegativeSqrtError) Error() string {
return fmt.Sprintf(&#34;math: square root of negative number %g&#34;, float64(f))
}
</pre>
}</pre>
<p>
A sophisticated caller can then use a
......@@ -176,8 +168,7 @@ returns when it encounters a syntax error parsing a JSON blob.
Offset int64 // error occurred after reading Offset bytes
}
func (e *SyntaxError) Error() string { return e.msg }
</pre>
func (e *SyntaxError) Error() string { return e.msg }</pre>
<p>
The <code>Offset</code> field isn't even shown in the default formatting of the
......@@ -186,14 +177,13 @@ messages:
</p>
<pre><!--{{code "progs/error.go" `/func decodeError/` `/STOP/`}}
--> if err := dec.Decode(&amp;val); err != nil {
-->if err := dec.Decode(&amp;val); err != nil {
if serr, ok := err.(*json.SyntaxError); ok {
line, col := findLine(f, serr.Offset)
return fmt.Errorf(&#34;%s:%d:%d: %v&#34;, f.Name(), line, col, err)
}
return err
}
</pre>
}</pre>
<p>
(This is a slightly simplified version of some
......@@ -226,14 +216,13 @@ up otherwise.
</p>
<pre><!--{{code "progs/error.go" `/func netError/` `/STOP/`}}
--> if nerr, ok := err.(net.Error); ok &amp;&amp; nerr.Temporary() {
-->if nerr, ok := err.(net.Error); ok &amp;&amp; nerr.Temporary() {
time.Sleep(1e9)
continue
}
if err != nil {
log.Fatal(err)
}
</pre>
}</pre>
<p>
<b>Simplifying repetitive error handling</b>
......@@ -269,8 +258,7 @@ func viewRecord(w http.ResponseWriter, r *http.Request) {
if err := viewTemplate.Execute(w, record); err != nil {
http.Error(w, err.Error(), 500)
}
}
</pre>
}</pre>
<p>
This function handles errors returned by the <code>datastore.Get</code>
......@@ -287,8 +275,7 @@ type that includes an <code>error</code> return value:
</p>
<pre><!--{{code "progs/error3.go" `/type appHandler/`}}
-->type appHandler func(http.ResponseWriter, *http.Request) error
</pre>
-->type appHandler func(http.ResponseWriter, *http.Request) error</pre>
<p>
Then we can change our <code>viewRecord</code> function to return errors:
......@@ -303,8 +290,7 @@ Then we can change our <code>viewRecord</code> function to return errors:
return err
}
return viewTemplate.Execute(w, record)
}
</pre>
}</pre>
<p>
This is simpler than the original version, but the <a
......@@ -319,8 +305,7 @@ To fix this we can implement the <code>http.Handler</code> interface's
if err := fn(w, r); err != nil {
http.Error(w, err.Error(), 500)
}
}
</pre>
}</pre>
<p>
The <code>ServeHTTP</code> method calls the <code>appHandler</code> function
......@@ -339,8 +324,7 @@ Now when registering <code>viewRecord</code> with the http package we use the
<pre><!--{{code "progs/error3.go" `/func init/` `/STOP/`}}
-->func init() {
http.Handle(&#34;/view&#34;, appHandler(viewRecord))
}
</pre>
}</pre>
<p>
With this basic error handling infrastructure in place, we can make it more
......@@ -360,16 +344,14 @@ To do this we create an <code>appError</code> struct containing an
Error error
Message string
Code int
}
</pre>
}</pre>
<p>
Next we modify the appHandler type to return <code>*appError</code> values:
</p>
<pre><!--{{code "progs/error4.go" `/type appHandler/`}}
-->type appHandler func(http.ResponseWriter, *http.Request) *appError
</pre>
-->type appHandler func(http.ResponseWriter, *http.Request) *appError</pre>
<p>
(It's usually a mistake to pass back the concrete type of an error rather than
......@@ -392,8 +374,7 @@ console:
c.Errorf(&#34;%v&#34;, e.Error)
http.Error(w, e.Message, e.Code)
}
}
</pre>
}</pre>
<p>
Finally, we update <code>viewRecord</code> to the new function signature and
......@@ -412,8 +393,7 @@ have it return more context when it encounters an error:
return &amp;appError{err, &#34;Can&#39;t display record&#34;, 500}
}
return nil
}
</pre>
}</pre>
<p>
This version of <code>viewRecord</code> is the same length as the original, but
......
......@@ -1690,8 +1690,7 @@ const (
EB
ZB
YB
)
</pre>
)</pre>
<p>
The ability to attach a method such as <code>String</code> to a
type makes it possible for such values to format themselves
......@@ -1718,8 +1717,7 @@ automatically for printing, even as part of a general type.
return fmt.Sprintf(&#34;%.2fKB&#34;, float64(b/KB))
}
return fmt.Sprintf(&#34;%.2fB&#34;, float64(b))
}
</pre>
}</pre>
<p>
(The <code>float64</code> conversions prevent <code>Sprintf</code>
from recurring back through the <code>String</code> method for
......@@ -1893,8 +1891,7 @@ func (s Sequence) String() string {
str += fmt.Sprint(elem)
}
return str + &#34;]&#34;
}
</pre>
}</pre>
<h3 id="conversions">Conversions</h3>
......@@ -3044,8 +3041,7 @@ value=&#34;Show QR&#34; name=qr&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
`
</pre>
`</pre>
<p>
The pieces up to <code>main</code> should be easy to follow.
The one flag sets a default HTTP port for our server. The template
......
......@@ -44,9 +44,8 @@ call.
</p>
<pre><!--{{code "progs/go1.go" `/greeting := ..byte/` `/append.*hello/`}}
--> greeting := []byte{}
greeting = append(greeting, []byte(&#34;hello &#34;)...)
</pre>
-->greeting := []byte{}
greeting = append(greeting, []byte(&#34;hello &#34;)...)</pre>
<p>
By analogy with the similar property of <code>copy</code>, Go 1
......@@ -55,8 +54,7 @@ slice; the conversion is no longer necessary:
</p>
<pre><!--{{code "progs/go1.go" `/append.*world/`}}
--> greeting = append(greeting, &#34;world&#34;...)
</pre>
-->greeting = append(greeting, &#34;world&#34;...)</pre>
<p>
<em>Updating</em>:
......@@ -97,7 +95,7 @@ All four of the initializations in this example are legal; the last one was ille
</p>
<pre><!--{{code "progs/go1.go" `/type Date struct/` `/STOP/`}}
--> type Date struct {
-->type Date struct {
month string
day int
}
......@@ -124,8 +122,7 @@ All four of the initializations in this example are legal; the last one was ille
{&#34;Feb&#34;, 14},
{&#34;Nov&#34;, 11},
{&#34;Dec&#34;, 25},
}
</pre>
}</pre>
<p>
<em>Updating</em>:
......@@ -152,8 +149,7 @@ func init() {
c := make(chan int)
go initializationFunction(c)
PackageGlobal = &lt;-c
}
</pre>
}</pre>
<p>
<em>Updating</em>:
......@@ -186,14 +182,13 @@ relatives now take and return a <code>rune</code>.
</p>
<pre><!--{{code "progs/go1.go" `/STARTRUNE/` `/ENDRUNE/`}}
--> delta := &#39;δ&#39; // delta has type rune.
-->delta := &#39;δ&#39; // delta has type rune.
var DELTA rune
DELTA = unicode.ToUpper(delta)
epsilon := unicode.ToLower(DELTA + 1)
if epsilon != &#39;δ&#39;+1 {
log.Fatal(&#34;inconsistent casing for Greek&#34;)
}
</pre>
}</pre>
<p>
<em>Updating</em>:
......@@ -236,8 +231,7 @@ function, <code>delete</code>. The call
</p>
<pre><!--{{code "progs/go1.go" `/delete\(m, k\)/`}}
--> delete(m, k)
</pre>
-->delete(m, k)</pre>
<p>
will delete the map entry retrieved by the expression <code>m[k]</code>.
......@@ -264,12 +258,11 @@ Code should not assume that the elements are visited in any particular order.
</p>
<pre><!--{{code "progs/go1.go" `/Sunday/` `/^ }/`}}
--> m := map[string]int{&#34;Sunday&#34;: 0, &#34;Monday&#34;: 1}
-->m := map[string]int{&#34;Sunday&#34;: 0, &#34;Monday&#34;: 1}
for name, value := range m {
// This loop should not assume Sunday will be visited first.
f(name, value)
}
</pre>
}</pre>
<p>
<em>Updating</em>:
......@@ -299,7 +292,7 @@ These examples illustrate the behavior.
</p>
<pre><!--{{code "progs/go1.go" `/sa :=/` `/then sc.0. = 2/`}}
--> sa := []int{1, 2, 3}
-->sa := []int{1, 2, 3}
i := 0
i, sa[i] = 1, 2 // sets i = 1, sa[0] = 2
......@@ -308,8 +301,7 @@ These examples illustrate the behavior.
sb[j], j = 2, 1 // sets sb[0] = 2, j = 1
sc := []int{1, 2, 3}
sc[0], sc[0] = 1, 2 // sets sc[0] = 1, then sc[0] = 2 (so sc[0] = 2 at end)
</pre>
sc[0], sc[0] = 1, 2 // sets sc[0] = 1, then sc[0] = 2 (so sc[0] = 2 at end)</pre>
<p>
<em>Updating</em>:
......@@ -417,7 +409,7 @@ As a result, structs and arrays can now be used as map keys:
</p>
<pre><!--{{code "progs/go1.go" `/type Day struct/` `/Printf/`}}
--> type Day struct {
-->type Day struct {
long string
short string
}
......@@ -427,8 +419,7 @@ As a result, structs and arrays can now be used as map keys:
Christmas: true,
Thanksgiving: true,
}
fmt.Printf(&#34;Christmas is a holiday: %t\n&#34;, holiday[Christmas])
</pre>
fmt.Printf(&#34;Christmas is a holiday: %t\n&#34;, holiday[Christmas])</pre>
<p>
Note that equality is still undefined for slices, for which the
......@@ -575,8 +566,7 @@ does for <code>String</code>, for easy printing of error values.
func (se *SyntaxError) Error() string {
return fmt.Sprintf(&#34;%s:%d: %s&#34;, se.File, se.Line, se.Message)
}
</pre>
}</pre>
<p>
All standard packages have been updated to use the new interface; the old <code>os.Error</code> is gone.
......@@ -595,8 +585,7 @@ to turn a string into an error. It replaces the old <code>os.NewError</code>.
</p>
<pre><!--{{code "progs/go1.go" `/ErrSyntax/`}}
--> var ErrSyntax = errors.New(&#34;syntax error&#34;)
</pre>
-->var ErrSyntax = errors.New(&#34;syntax error&#34;)</pre>
<p>
<em>Updating</em>:
......@@ -677,8 +666,7 @@ func sleepUntil(wakeup time.Time) {
delta := wakeup.Sub(now) // A Duration.
log.Printf(&#34;Sleeping for %.3fs&#34;, delta.Seconds())
time.Sleep(delta)
}
</pre>
}</pre>
<p>
The new types, methods, and constants have been propagated through
......
This diff is collapsed.
......@@ -113,6 +113,8 @@ func code(file string, arg ...interface{}) (string, error) {
default:
return "", fmt.Errorf("incorrect code invocation: code %q %q", file, arg)
}
// Trim spaces from output.
text = strings.TrimSpace(text)
// Replace tabs by spaces, which work better in HTML.
text = strings.Replace(text, "\t", " ", -1)
// Escape the program text for HTML.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment