From b50ed022f5d735ee341ebdf15a9a151f4b6e5494 Mon Sep 17 00:00:00 2001
From: Robert Griesemer
-Except in a communications clause of a select statement,
-sending or receiving from a nil
channel causes a
+Receiving from a nil
channel causes a
run-time panic.
@@ -3508,7 +3475,7 @@ Statement = FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt | DeferStmt . -SimpleStmt = EmptyStmt | ExpressionStmt | IncDecStmt | Assignment | ShortVarDecl . +SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl . @@ -3543,7 +3510,7 @@ Error: log.Crash("error encountered")
-Function calls, method calls, and channel operations +Function calls, method calls, and receive operations can appear in statement context.
@@ -3553,11 +3520,44 @@ ExpressionStmt = Expression .-f(x+y) +h(x+y) +f.Close() <-ch+
+A send statement sends a value on a channel. +The channel expression must be of channel type +and the type of the value must be assignable +to the channel's element type. +
+ ++SendStmt = Channel "<-" Expression . +Channel = Expression . ++ +
+Both the channel and the value expression are evaluated before communication +begins. Communication blocks until the send can proceed, at which point the +value is transmitted on the channel. +A send on an unbuffered channel can proceed if a receiver is ready. +A send on a buffered channel can proceed if there is room in the buffer. +
+ ++ch <- 3 ++ +
+Sending to a nil
channel causes a
+run-time panic.
+
@@ -4076,18 +4076,19 @@ cases all referring to communication operations.
SelectStmt = "select" "{" { CommClause } "}" . CommClause = CommCase ":" { Statement ";" } . -CommCase = "case" ( SendExpr | RecvExpr) | "default" . -SendExpr = Expression "<-" Expression . -RecvExpr = [ Expression ( "=" | ":=" ) ] "<-" Expression . +CommCase = "case" ( SendStmt | RecvStmt ) | "default" . +RecvStmt = [ Expression ( "=" | ":=" ) ] RecvExpr . +RecvExpr = Expression .
-For all the send and receive expressions in the "select"
+RecvExpr must be a receive operation.
+For all the cases in the "select"
statement, the channel expressions are evaluated in top-to-bottom order, along with
-any expressions that appear on the right hand side of send expressions.
+any expressions that appear on the right hand side of send statements.
A channel may be nil
,
which is equivalent to that case not
being present in the select statement
@@ -4398,7 +4399,7 @@ sent values have been received, receive operations will return
the zero value for the channel's type without blocking.
After at least one such zero value has been