From e93891f348a8cd74272c1060458a2e64cc0543ab Mon Sep 17 00:00:00 2001
From: Oling Cat
There is no automatic fall through, but cases can be presented
in comma-separated lists.
+
Here's a comparison routine for byte arrays that uses two
func shouldEscape(c byte) bool {
switch c {
@@ -727,6 +728,7 @@ func shouldEscape(c byte) bool {
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 a nil
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, although Append
can modify the elements of slice
, 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
...
after v
in the nested call to Sprintln
to tell the
compiler to treat v
as a list of arguments; otherwise it would just pass
v
as a single slice argument.
+
There's even more to printing than we've covered here. See the godoc
documentation
for package fmt
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 standard Write
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 an
http.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