英文:
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 <-[]chan string) <-chan string {
c := make(chan string)
for i := range set {
go func() { for {c <-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("Joe"), mylib.Boring("Ann")}
c := fanIn(set)
if I can do this
func fanIn(input1, input2 <-chan string) <-chan string {
I assume that it should be possible to have slice or array of "<-chan string"
updated:
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.")
}
答案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 []<-chan string) <-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're sending the channel itself to the other channel
go func() { for {c <- <- set[i]} }()
}
return c
}
BTW for the sake of readability, I'd write it as:
go func() {
for {
x := <-set[i]
c <- 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 []<-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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论