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

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

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

问题

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

func fanIn(set <-[]chan string) <-chan string {
    c := make(chan string)

    for i := range set {
        go func() { for {c <-set[i]} }()
    }
    return c
}

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

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

如果我可以这样做:

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

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

更新后的代码:

func fanIn(set []<-chan string) <-chan string {
    c := make(chan string)

    for i := range set {
        go func() {
            for {
                x := <-set[i]
                c <- x
            }
        }()
    }

    return c
}

func main() {
    set := []<-chan string{mylib.Boring("Joe"), mylib.Boring("Ann"), mylib.Boring("Max")}
    c := fanIn(set)
    for i := 0; i < 10; i++ {
        fmt.Println(<-c)
    }
    fmt.Println("You're boring: I'm leaving.")
}

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

英文:

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

func fanIn(set &lt;-[]chan string) &lt;-chan string {
	c := make(chan string)

	for i := range set {
		go func() { for {c &lt;-set[i]} }()
	}
	return c
}

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

example of call

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

if I can do this

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:

func fanIn(set []&lt;-chan string) &lt;-chan string {
	c := make(chan string)

	for i := range set {
		go func() {
			for {
                x := &lt;-set[i]
				c &lt;- x
			}
		}()
	}

	return c
}

func main() {
	set := []&lt;-chan string{mylib.Boring(&quot;Joe&quot;), mylib.Boring(&quot;Ann&quot;), mylib.Boring(&quot;Max&quot;)}
	c := fanIn(set)
	for i := 0; i &lt; 10; i++ {
		fmt.Println(&lt;-c)
	}
	fmt.Println(&quot;You&#39;re boring: I&#39;m leaving.&quot;)
}

答案1

得分: 4

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

func fanIn(set []<-chan string) <-chan string {
    c := make(chan string)

    for i := range set {
        // 这里是主要的改动 - 你从一个通道接收并发送到另一个通道。
        // 你之前的写法是将通道本身发送到另一个通道
        go func() { for {c <- <- set[i]} }()
    }
    return c
}

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

go func() {
    for {
        x := <-set[i]
        c <- x
    }
}()

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

func fanIn(set []<-chan string) <-chan string {
    c := make(chan string)

    for i := range set {
        go func(in <-chan string) {
            for {
                x := <-in
                c <- x
            }
        }(set[i])
    }
    return c
}
英文:

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

func fanIn(set []&lt;-chan string) &lt;-chan string {
    c := make(chan string)


    for i := range set {
        // here is the main change - you receive from one channel and send to one.
        // the way you wrote it, you&#39;re sending the channel itself to the other channel
        go func() { for {c &lt;- &lt;- set[i]} }()
    }
    return c
}

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

	go func() {
		for {
			x := &lt;-set[i]
			c &lt;- x
		}
	}()

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:

func fanIn(set []&lt;-chan string) &lt;-chan string {
	c := make(chan string)

    for i := range set {
		go func(in &lt;-chan string) {
			for {
				x := &lt;- in
				c &lt;- x
			}
		}(set[i])
	}
	return c
}

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:

确定