From: Rob Pike Date: Mon, 16 Sep 2013 21:41:45 +0000 (+1000) Subject: effective_go: add a discussion of labeled break and continue X-Git-Tag: go1.2rc2~191 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2a5dcfafec11744d55692838912901c58ba43bd2;p=gostls13.git effective_go: add a discussion of labeled break and continue Fixes #5725. R=golang-dev, iant CC=golang-dev https://golang.org/cl/13705044 --- diff --git a/doc/effective_go.html b/doc/effective_go.html index 8c66fa246f..7d2a904e50 100644 --- a/doc/effective_go.html +++ b/doc/effective_go.html @@ -506,6 +506,8 @@ slightly generalized switch is more flexible; if and switch accept an optional initialization statement like that of for; +break and continue statements +take an optional label to identify what to break or continue; and there are new control structures including a type switch and a multiway communications multiplexer, select. The syntax is also slightly different: @@ -781,7 +783,46 @@ func shouldEscape(c byte) bool {

-Here's a comparison routine for byte slices that uses two +Although they are not nearly as common in Go as some other C-like +languages, break statements can be used to terminate +a switch early. +Sometimes, though, it's necessary to break out of a surrounding loop, +not the switch, and in Go that can be accomplished by putting a label +on the loop and "breaking" to that label. +This example shows both uses. +

+ +
+Loop:
+	for n := 0; n < len(src); n += size {
+		case src[n] < sizeOne:
+			if validateOnly {
+				break
+			}
+			size = 1
+			update(src[n])
+
+		case src[n] < sizeTwo:
+			if n+1 >= len(src) {
+				err = errShortInput
+				break Loop
+			}
+			if validateOnly {
+				break
+			}
+			size = 2
+			update(src[n] + src[n+1]<<shift)
+		}
+	}
+
+ +

+Of course, the continue statement also accepts an optional label +but it applies only to loops. +

+ +

+To close this section, here's a comparison routine for byte slices that uses two switch statements: