how can I declare a slice of chan (channels) in func

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

how can I declare a slice of chan (channels) in func

问题

我正在尝试编写这样的函数,但我无法声明通道的切片。

  1. func fanIn(set <-[]chan string) <-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. go func() { for {c <-set[i]} }()
  5. }
  6. return c
  7. }

在Go语言中,是否可以将通道的切片作为参数?例如调用示例:

  1. set := [2]chan string{mylib.Boring("Joe"), mylib.Boring("Ann")}
  2. c := fanIn(set)

如果我可以这样做:

  1. func fanIn(input1, input2 <-chan string) <-chan string {

我认为应该可以有一个<-chan string的切片或数组。

更新后的代码:

  1. func fanIn(set []<-chan string) <-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. go func() {
  5. for {
  6. x := <-set[i]
  7. c <- x
  8. }
  9. }()
  10. }
  11. return c
  12. }
  13. func main() {
  14. set := []<-chan string{mylib.Boring("Joe"), mylib.Boring("Ann"), mylib.Boring("Max")}
  15. c := fanIn(set)
  16. for i := 0; i < 10; i++ {
  17. fmt.Println(<-c)
  18. }
  19. fmt.Println("You're boring: I'm leaving.")
  20. }

请注意,我只翻译了你提供的代码部分。

英文:

I'm trying to write function like this, but I can't declare slice of channels

  1. func fanIn(set &lt;-[]chan string) &lt;-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. go func() { for {c &lt;-set[i]} }()
  5. }
  6. return c
  7. }

is it possible in Go to have a slice of channels as argument?

example of call

  1. set := [2]chan string{mylib.Boring(&quot;Joe&quot;), mylib.Boring(&quot;Ann&quot;)}
  2. c := fanIn(set)

if I can do this

  1. func fanIn(input1, input2 &lt;-chan string) &lt;-chan string {

I assume that it should be possible to have slice or array of "<-chan string"

updated:

  1. func fanIn(set []&lt;-chan string) &lt;-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. go func() {
  5. for {
  6. x := &lt;-set[i]
  7. c &lt;- x
  8. }
  9. }()
  10. }
  11. return c
  12. }
  13. func main() {
  14. set := []&lt;-chan string{mylib.Boring(&quot;Joe&quot;), mylib.Boring(&quot;Ann&quot;), mylib.Boring(&quot;Max&quot;)}
  15. c := fanIn(set)
  16. for i := 0; i &lt; 10; i++ {
  17. fmt.Println(&lt;-c)
  18. }
  19. fmt.Println(&quot;You&#39;re boring: I&#39;m leaving.&quot;)
  20. }

答案1

得分: 4

我稍微修改了你的函数的语法,现在可以编译了:

  1. func fanIn(set []<-chan string) <-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. // 这里是主要的改动 - 你从一个通道接收并发送到另一个通道。
  5. // 你之前的写法是将通道本身发送到另一个通道
  6. go func() { for {c <- <- set[i]} }()
  7. }
  8. return c
  9. }

顺便提一下,为了可读性,我会这样写:

  1. go func() {
  2. for {
  3. x := <-set[i]
  4. c <- x
  5. }
  6. }()

编辑:你原来的代码在 goroutine 中使用了 set[i],导致它们都从最后一个通道读取。这是修复后的版本:

  1. func fanIn(set []<-chan string) <-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. go func(in <-chan string) {
  5. for {
  6. x := <-in
  7. c <- x
  8. }
  9. }(set[i])
  10. }
  11. return c
  12. }
英文:

I fixed the syntax in your function a bit, it compiles now:

  1. func fanIn(set []&lt;-chan string) &lt;-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. // here is the main change - you receive from one channel and send to one.
  5. // the way you wrote it, you&#39;re sending the channel itself to the other channel
  6. go func() { for {c &lt;- &lt;- set[i]} }()
  7. }
  8. return c
  9. }

BTW for the sake of readability, I'd write it as:

  1. go func() {
  2. for {
  3. x := &lt;-set[i]
  4. c &lt;- x
  5. }
  6. }()

EDIT: Your original code had the problem of using set[i] inside the goroutine, causing them all to read from the last channel. here's a fixed version:

  1. func fanIn(set []&lt;-chan string) &lt;-chan string {
  2. c := make(chan string)
  3. for i := range set {
  4. go func(in &lt;-chan string) {
  5. for {
  6. x := &lt;- in
  7. c &lt;- x
  8. }
  9. }(set[i])
  10. }
  11. return c
  12. }

huangapple
  • 本文由 发表于 2014年10月10日 17:55:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/26296683.html
匿名

发表评论

匿名网友

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

确定