英文:
Explain this Go statement
问题
我在一本 Go 书中找到了这个代码,但在语言的语法中找不到它。有人可以解释一下吗?它本质上是某种"标签"吗?
return (<-reply).(int)
英文:
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)
答案1
得分: 10
在该语句中,有三个不同的语言特性同时起作用。
return
从函数中返回一个值<-reply
从名为 reply 的通道中读取一个值var.(type)
断言一个接口包含该类型。
因此,将它们组合在一起,你正在从 reply 通道中读取一个接口值,断言该值是一个整数,并返回该整数值。
英文:
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 replyvar.(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.
答案2
得分: 9
《Go编程语言规范》
接收操作符
对于通道类型的操作数ch,接收操作<-ch的值是从通道ch接收到的值。通道的方向必须允许接收操作,并且接收操作的类型是通道的元素类型。该表达式会阻塞,直到有值可用。从nil通道接收会永远阻塞。对已关闭的通道进行接收操作总是可以立即进行,之前发送的任何值都会在接收到之后返回元素类型的零值。
v1 := <-ch
v2 = <-ch
f(<-ch)
<-strobe // 等待时钟脉冲并丢弃接收到的值
在赋值或初始化的特殊形式中使用的接收表达式
x, ok = <-ch
x, ok := <-ch
var x, ok = <-ch
会产生一个额外的无类型布尔结果,报告通信是否成功。如果接收到的值是通过成功的发送操作传递到通道的,则ok的值为true;如果通道已关闭且为空,则ok的值为false,表示生成的是零值。
类型断言
对于接口类型的表达式x和类型T,主表达式
x.(T)
断言x不是nil,并且x中存储的值是类型T。x.(T)的表示法称为类型断言。
如果类型断言成立,表达式的值是x中存储的值,其类型是T。如果类型断言不成立,将发生运行时恐慌。换句话说,尽管x的动态类型只在运行时才知道,但在正确的程序中,x.(T)的类型被知道是T。
返回语句
函数F中的"return"语句终止F的执行,并可选择提供一个或多个结果值。在F返回给其调用者之前,任何被F延迟的函数都会被执行。
对于return (<-reply).(int)
,
<-reply
从通道reply
接收一个值。
(<-reply).(int)
断言从通道reply
接收到的值是int
类型。
return (<-reply).(int)
返回从通道reply
接收到的int
值,并终止函数或方法的执行。
英文:
> 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.
答案3
得分: 0
在一个简单的示例中,return (<-reply).(int)
的代码如下:
package main
import "fmt"
func F() int {
reply := make(chan interface{})
go func() { reply <- 1 }()
return (<-reply).(int)
}
func main() {
fmt.Println(F())
}
请注意,这段代码是使用Go语言编写的。它的功能是创建一个通道(channel)reply
,然后在一个goroutine中将值1发送到通道中。接着,在主函数中调用F()
函数,并将其返回值打印出来。
在F()
函数中,(<-reply).(int)
表示从通道reply
中接收一个值,并将其转换为整数类型。然后,这个整数值被作为F()
函数的返回值。
因此,这段代码的输出结果将是1。
英文:
return (<-reply).(int)
in a simple example:
package main
import "fmt"
func F() int {
reply := make(chan interface{})
go func() { reply <- 1 }()
return (<-reply).(int)
}
func main() {fmt.Println(F())}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论