summaryrefslogtreecommitdiff
path: root/doc/effective_go.html
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-02-14 13:23:51 +0100
committerOndřej Surý <ondrej@sury.org>2011-02-14 13:23:51 +0100
commit758ff64c69e34965f8af5b2d6ffd65e8d7ab2150 (patch)
tree6d6b34f8c678862fe9b56c945a7b63f68502c245 /doc/effective_go.html
parent3e45412327a2654a77944249962b3652e6142299 (diff)
downloadgolang-upstream/2011-02-01.1.tar.gz
Imported Upstream version 2011-02-01.1upstream/2011-02-01.1
Diffstat (limited to 'doc/effective_go.html')
-rw-r--r--doc/effective_go.html51
1 files changed, 31 insertions, 20 deletions
diff --git a/doc/effective_go.html b/doc/effective_go.html
index 26e317b5d..3f6f89b8b 100644
--- a/doc/effective_go.html
+++ b/doc/effective_go.html
@@ -1124,14 +1124,14 @@ you can pass a pointer to the array.
</p>
<pre>
-func Sum(a *[3]float) (sum float) {
+func Sum(a *[3]float64) (sum float64) {
for _, v := range *a {
sum += v
}
return
}
-array := [...]float{7.0, 8.5, 9.1}
+array := [...]float64{7.0, 8.5, 9.1}
x := Sum(&amp;array) // Note the explicit address-of operator
</pre>
@@ -1233,7 +1233,8 @@ Maps are a convenient and powerful built-in data structure to associate
values of different types.
The key can be of any type for which the equality operator is defined,
such as integers,
-floats, strings, pointers, and interfaces (as long as the dynamic type
+floating point and complex numbers,
+strings, pointers, and interfaces (as long as the dynamic type
supports equality). Structs, arrays and slices cannot be used as map keys,
because equality is not defined on those types.
Like slices, maps are a reference type. If you pass a map to a function
@@ -1652,7 +1653,7 @@ correctness of the program state before real execution begins.
<pre>
func init() {
if USER == "" {
- log.Exit("$USER not set")
+ log.Fatal("$USER not set")
}
if HOME == "" {
HOME = "/usr/" + USER
@@ -1806,7 +1807,7 @@ Because the two types (<code>Sequence</code> and <code>[]int</code>)
are the same if we ignore the type name, it's legal to convert between them.
The conversion doesn't create a new value, it just temporarily acts
as though the existing value has a new type.
-(There are other legal conversions, such as from integer to float, that
+(There are other legal conversions, such as from integer to floating point, that
do create a new value.)
</p>
<p>
@@ -2525,39 +2526,49 @@ var serverChan = make(chan *Buffer)
func client() {
for {
- b, ok := &lt;-freeList // grab a buffer if available
- if !ok { // if not, allocate a new one
+ var b *Buffer
+ // Grab a buffer if available; allocate if not.
+ select {
+ case b = &lt;-freeList:
+ // Got one; nothing more to do.
+ default:
+ // None free, so allocate a new one.
b = new(Buffer)
}
- load(b) // read next message from the net
- serverChan &lt;- b // send to server
+ load(b) // Read next message from the net.
+ serverChan &lt;- b // Send to server.
}
}
</pre>
<p>
-The server loop receives messages from the client, processes them,
+The server loop receives each message from the client, processes it,
and returns the buffer to the free list.
</p>
<pre>
func server() {
for {
- b := &lt;-serverChan // wait for work
+ b := &lt;-serverChan // Wait for work.
process(b)
- _ = freeList &lt;- b // reuse buffer if room
+ // Reuse buffer if there's room.
+ select {
+ case freeList &lt;- b:
+ // Buffer on free list; nothing more to do.
+ default:
+ // Free list full, just carry on.
+ }
}
}
</pre>
<p>
-The client's non-blocking receive from <code>freeList</code> obtains a
-buffer if one is available; otherwise the client allocates
-a fresh one.
-The server's non-blocking send on freeList puts <code>b</code> back
+The client attempts to retrieve a buffer from <code>freeList</code>;
+if none is available, it allocates a fresh one.
+The server's send to <code>freeList</code> puts <code>b</code> back
on the free list unless the list is full, in which case the
buffer is dropped on the floor to be reclaimed by
the garbage collector.
-(The assignment of the send operation to the blank identifier
-makes it non-blocking but ignores whether
-the operation succeeded.)
+(The <code>default</code> clauses in the <code>select</code>
+statements execute when no other case is ready,
+meaning that the <code>selects</code> never block.)
This implementation builds a leaky bucket free list
in just a few lines, relying on the buffered channel and
the garbage collector for bookkeeping.
@@ -2860,7 +2871,7 @@ func main() {
http.Handle("/", http.HandlerFunc(QR))
err := http.ListenAndServe(*addr, nil)
if err != nil {
- log.Exit("ListenAndServe:", err)
+ log.Fatal("ListenAndServe:", err)
}
}