diff --git a/doc/effective_go.html b/doc/effective_go.html old mode 100644 new mode 100755 index 6ad7ee3c224..2ea3dc751bb --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -714,6 +714,7 @@ func unhex(c byte) byte {
There is no automatic fall through, but cases can be presented in comma-separated lists. +
func shouldEscape(c byte) bool { switch c { @@ -727,6 +728,7 @@ func shouldEscape(c byte) bool {Here's a comparison routine for byte arrays that uses two
switch
statements: +// Compare returns an integer comparing the two byte arrays, // lexicographically. @@ -1180,6 +1182,7 @@ structure with length 10 and a capacity of 100 pointing at the first for more information.) In contrast,new([]int)
returns a pointer to a newly allocated, zeroed slice structure, that is, a pointer to anil
slice value. +These examples illustrate the difference between
+new
and @@ -1330,6 +1333,8 @@ func Append(slice, data[]byte) []byte { We must return the slice afterwards because, althoughAppend
can modify the elements ofslice
, the slice itself (the run-time data structure holding the pointer, length, and capacity) is passed by value. +The idea of appending to a slice is so useful it's captured by the
append
built-in function. To understand that function's @@ -1545,6 +1550,7 @@ a space in the format (% x
) it puts spaces between the bytes.Another handy format is
%T
, which prints the type of a value. +fmt.Printf("%T\n", timeZone)@@ -1606,6 +1612,7 @@ func Println(v ...interface{}) { We write...
afterv
in the nested call toSprintln
to tell the compiler to treatv
as a list of arguments; otherwise it would just passv
as a single slice argument. +There's even more to printing than we've covered here. See the
godoc
documentation for packagefmt
for the details. @@ -1783,6 +1790,7 @@ func init() {Methods can be defined for any named type that is not a pointer or an interface; the receiver does not have to be a struct. +
In the discussion of slices above, we wrote an
Append
function. We can define it as a method on slices instead. To do @@ -2012,6 +2020,7 @@ Those methods include the standardWrite
method, so an can be used.Request
is a struct containing a parsed representation of the request from the client. +For brevity, let's ignore POSTs and assume HTTP requests are always GETs; that simplification does not affect the way the handlers are @@ -2034,6 +2043,7 @@ func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) { (Keeping with our theme, note how
Fprintf
can print to anhttp.ResponseWriter
.) For reference, here's how to attach such a server to a node on the URL tree. +import "net/http" ... @@ -2187,6 +2197,7 @@ what aReader
does and what aWriter
does; it is a union of the embedded interfaces (which must be disjoint sets of methods). Only interfaces can be embedded within interfaces. +The same basic idea applies to structs, but with more far-reaching implications. The
bufio
package has two struct types, @@ -2378,10 +2389,11 @@ exits, silently. (The effect is similar to the Unix shell's background.)-go list.Sort() // run list.Sort concurrently; don't wait for it. +go list.Sort() // run list.Sort concurrently; don't wait for it.A function literal can be handy in a goroutine invocation. +
func Announce(message string, delay time.Duration) { go func() { @@ -2393,6 +2405,7 @@ func Announce(message string, delay time.Duration) {In Go, function literals are closures: the implementation makes sure the variables referred to by the function survive as long as they are active. +
These examples aren't too practical because the functions have no way of signaling completion. For that, we need channels. @@ -2425,7 +2438,7 @@ c := make(chan int) // Allocate a channel. // Start the sort in a goroutine; when it completes, signal on the channel. go func() { list.Sort() - c <- 1 // Send a signal; value does not matter. + c <- 1 // Send a signal; value does not matter. }() doSomethingForAWhile() <-c // Wait for sort to finish; discard sent value. @@ -2494,6 +2507,7 @@ One of the most important properties of Go is that a channel is a first-class value that can be allocated and passed around like any other. A common use of this property is to implement safe, parallel demultiplexing. +
In the example in the previous section,
handle
was an idealized handler for a request but we didn't define the @@ -3026,7 +3040,7 @@ TODOverifying implementation type Color uint32 - + // Check that Color implements image.Color and image.Image var _ image.Color = Black var _ image.Image = Black