From b4f297238cb9c16fdd65f8a56b4f4ee6aad7aa8e Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 30 Mar 2010 11:21:50 -0700 Subject: Effective Go: update maps description regarding lookup of nonexistent entry. R=rsc, gri, iant CC=golang-dev http://codereview.appspot.com/821044 --- doc/effective_go.html | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'doc') diff --git a/doc/effective_go.html b/doc/effective_go.html index 728e07be1..ce5fcb99d 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -1121,9 +1121,23 @@ var timeZone = map[string] int {

Assigning and fetching map values looks syntactically just like doing the same for arrays except that the index doesn't need to -be an integer. An attempt to fetch a map value with a key that -is not present in the map will cause the program to crash, but -there is a way to do so safely using a multiple assignment. +be an integer. +

+
+offset := timeZone["EST"]
+
+

+An attempt to fetch a map value with a key that +is not present in the map will return the zero value for the type +of the entries +in the map. For instance, if the map contains integers, looking +up a non-existent key will return 0. +

+

+Sometimes you need to distinguish a missing entry from +a zero value. Is there an entry for "UTC" +or is that zero value because it's not in the map at all? +You can discriminate with a form of multiple assignment.

 var seconds int
@@ -1136,7 +1150,7 @@ In this example, if tz is present, seconds
 will be set appropriately and ok will be true; if not,
 seconds will be set to zero and ok will
 be false.
-Here's a function that puts it together:
+Here's a function that puts it together with a nice error report:
 

 func offset(tz string) int {
@@ -1151,7 +1165,7 @@ func offset(tz string) int {
 To test for presence in the map without worrying about the actual value,
 you can use the blank identifier, a simple underscore (_).
 The blank identifier can be assigned or declared with any value of any type, with the
-value discarded harmlessly.  For testing presence in a map, use the blank
+value discarded harmlessly.  For testing just presence in a map, use the blank
 identifier in place of the usual variable for the value.
 

@@ -1166,6 +1180,7 @@ from the map.
 
 timeZone["PDT"] = 0, false  // Now on Standard Time
 
+

Printing

-- cgit v1.2.3