将 chan 转换为非 chan 的方法在 Golang 中是什么?

huangapple go评论75阅读模式
英文:

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())
}

Playground

英文:

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 &lt;- funcWithChanResult() 
// invalid operation: result &lt;- funcWithChanResult() (send to non-chan type int)

Full code:

package main

import (
	&quot;fmt&quot;
	&quot;time&quot;
)

func getIntSlowly() int {
	time.Sleep(time.Millisecond * 500)
	return 123
}

func funcWithChanResult() chan int {
	chanint := make(chan int)
	go func() {
		chanint &lt;- getIntSlowly()
	}()
  	return chanint
}

func funcWithNonChanResult() int {
    var result int
	result = funcWithChanResult() 
	// result &lt;- funcWithChanResult() 
	return result
}

func main() {
	fmt.Println(&quot;Received first int:&quot;, &lt;-funcWithChanResult())
	fmt.Println(&quot;Received second int:&quot;, funcWithNonChanResult())
}

Playground

答案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 = &lt;- ch

Or more compact:

result := &lt;- funcWithChanResult()

Combine this with the return statement:

func funcWithNonChanResult() int {
	return &lt;-funcWithChanResult()
}

Output (as expected):

Received first int: 123
Received second int: 123

Try your modified, working example on the Go Playground.

huangapple
  • 本文由 发表于 2015年6月16日 14:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/30860644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定