diff --git a/doc/go_spec.html b/doc/go_spec.html index 11596ae5b6..92776959c9 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -3272,9 +3272,9 @@ case x < 0: return -x default: return x } -switch { // missing expression means "true" -case x < y: f1(); -case x < z: f2(); +switch { // missing expression means "true" +case x < y: f1(); +case x < z: f2(); case x == 4: f3(); } @@ -3283,9 +3283,8 @@ case x == 4: f3();
A type switch compares types rather than values. It is otherwise similar
-to an expression switch. It is introduced by special
-notation in the form of a simple declaration whose right hand side
-has the form of a type assertion (§Type assertions)
+to an expression switch. It is marked by a special switch expression which
+has the form of a type assertion
using the reserved word type rather than an actual type.
Cases then match literal types against the dynamic type of the expression
in the type assertion.
@@ -3293,23 +3292,30 @@ in the type assertion.
TypeSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
-TypeSwitchGuard = identifier ":=" Expression "." "(" "type" ")" .
+TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" .
TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
TypeSwitchCase = "case" Type | "default" .
-As a special case, the type in the type switch case may be an
-identifier denoting the predeclared constant nil
-(§Predeclared identifiers).
-If the interface value equals nil,
-only an explict nil case or "default"
-case will execute.
+The TypeSwitchGuard may include a
+short variable declaration.
+When that form is used, the variable is declared in each clause.
+In clauses with a case listing exactly one type, the variable
+has that type; otherwise, the variable has the type of the expression
+in the TypeSwitchGuard.
-Given a function f
-that returns a value of interface type,
+The type in a case may be nil
+(§Predeclared identifiers);
+that case is used when the expression in the TypeSwitchGuard
+is a nil interface value.
+
+Given a function f that returns
+a value of type interface{},
the following type switch:
-In a type switch, the guard is mandatory, -there can be only one type per "case", and -the "fallthrough" statement is not allowed. -
- -+
The type switch guard may be preceded by a simple statement, which executes before the guard is evaluated. +
+ ++The "fallthrough" statement is not permitted in a type switch.