英文:
Convert chan to non chan in golang
问题
是否可以让函数funcWithNonChanResult
具有以下接口:
func funcWithNonChanResult() int {
如果我想要它使用具有以下接口的函数funcWithChanResult
:
func funcWithChanResult() chan int {
换句话说,我是否可以将chan int
转换为int
?或者我必须在所有使用funcWithChanResult
的函数中使用chan int
作为返回类型?
目前,我尝试了以下方法:
result = funcWithChanResult()
// 无法将类型为chan int的funcWithChanResult()分配给类型为int的result
result <- funcWithChanResult()
// 对非通道类型int进行发送操作:result <- funcWithChanResult()
完整代码:
```go
package main
import (
"fmt"
"time"
)
func getIntSlowly() int {
time.Sleep(time.Millisecond * 500)
return 123
}
func funcWithChanResult() chan int {
chanint := make(chan int)
go func() {
chanint <- getIntSlowly()
}()
return chanint
}
func funcWithNonChanResult() int {
var result int
result = funcWithChanResult()
// result <- funcWithChanResult()
return result
}
func main() {
fmt.Println("Received first int:", <-funcWithChanResult())
fmt.Println("Received second int:", funcWithNonChanResult())
}
英文:
Is it possible to let function funcWithNonChanResult
have the following interface:
func funcWithNonChanResult() int {
And if I want it to use function funcWithChanResult
with the interface:
func funcWithChanResult() chan int {
In other words, can I somehow convert chan int
to int
? Or I must have chan int
result type in all the functions which use funcWithChanResult
?
Currently, I tried these methods:
result = funcWithChanResult()
// cannot use funcWithChanResult() (type chan int) as type int in assignment
result <- funcWithChanResult()
// invalid operation: result <- funcWithChanResult() (send to non-chan type int)
Full code:
package main
import (
"fmt"
"time"
)
func getIntSlowly() int {
time.Sleep(time.Millisecond * 500)
return 123
}
func funcWithChanResult() chan int {
chanint := make(chan int)
go func() {
chanint <- getIntSlowly()
}()
return chanint
}
func funcWithNonChanResult() int {
var result int
result = funcWithChanResult()
// result <- funcWithChanResult()
return result
}
func main() {
fmt.Println("Received first int:", <-funcWithChanResult())
fmt.Println("Received second int:", funcWithNonChanResult())
}
答案1
得分: 9
一个chan int
是一个包含int
值的通道,它不是一个单独的int
值,而是一个int
值的来源(或者也可以是一个目标,在你的情况下你将其用作来源)。
因此,你不能将chan int
转换为int
。你可以做的,也可能是你的意思是使用从chan int
接收到的值(类型为int
)作为int
值。
这不是一个问题:
var result int
ch := funcWithChanResult()
result = <- ch
或者更简洁一些:
result := <- funcWithChanResult()
将其与return
语句结合使用:
func funcWithNonChanResult() int {
return <-funcWithChanResult()
}
输出(如预期):
接收到的第一个int值:123
接收到的第二个int值:123
在Go Playground上尝试你修改后的可工作示例。
英文:
A chan int
is a channel of int
values, it is not a single int
value but a source of int
values (or also a target, but in your case you use it as source).
So therefore you can't convert chan int
to int
. What you can do and probably what you mean is use a value (of type int
) received from a chan int
as an int
value.
This is not a problem:
var result int
ch := funcWithChanResult()
result = <- ch
Or more compact:
result := <- funcWithChanResult()
Combine this with the return
statement:
func funcWithNonChanResult() int {
return <-funcWithChanResult()
}
Output (as expected):
Received first int: 123
Received second int: 123
Try your modified, working example on the Go Playground.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论