解释此Go语句

I found this is in a Go book and can not find it in the syntax of the language. Can anyone explain this? Is it essentially a "tag" of some sort?

return (<-reply).(int)

You have three things going on in that statement that are different language features all working together.

  • return returns a value from a function
  • <-reply reads a value from the channel named reply
  • var.(type) asserts an interface contains that type.

So putting them all together, you're reading an interface value from the reply channel, asserting that value is an integer, and then returning that integer value.

The Go Programming Language Specification

Receive operator

For an operand ch of channel type, the value of the receive operation <-ch is the value received from the channel ch. The channel direction must permit receive operations, and the type of the receive operation is the element type of the channel. The expression blocks until a value is available. Receiving from a nil channel blocks forever. A receive operation on a closed channel can always proceed immediately, yielding the element type's zero value after any previously sent values have been received.

v1 := <-ch
v2 = <-ch
f(<-ch)
<-strobe  // wait until clock pulse and discard received value

A receive expression used in an assignment or initialization of the special form

x, ok = <-ch
x, ok := <-ch
var x, ok = <-ch

yields an additional untyped boolean result reporting whether the communication succeeded. The value of ok is true if the value received was delivered by a successful send operation to the channel, or false if it is a zero value generated because the channel is closed and empty.

Type assertions

For an expression x of interface type and a type T, the primary expression

x.(T)

asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

If the type assertion holds, the value of the expression is the value stored in x and its type is T. If the type assertion is false, a run-time panic occurs. In other words, even though the dynamic type of x is known only at run time, the type of x.(T) is known to be T in a correct program.

Return statements

A "return" statement in a function F terminates the execution of F, and optionally provides one or more result values. Any functions deferred by F are executed before F returns to its caller.

For return (<-reply).(int),

<-reply receives a value from channel reply.

(<-reply).(int) asserts that the value received from channel reply is of type int.

return (<-reply).(int) returns the int value received from channel reply and terminates the function or method.