英文:
go function that accepts any channel
问题
我想在Go语言中编写一个函数,该函数以通用通道作为输入。我希望它能够接受任何类型的通道作为输入。我尝试使用func myFunc(channel chan interface{})
,但是当我尝试将我的通道传递给它时,出现了编译错误。在Go语言中有没有办法实现这个功能?
---编辑---
有人要求我提供一个最小工作示例,所以我想在下面分享一些代码。我希望的是类似以下的代码:
package main
func SendToChannel(arg interface{}, channel chan interface{}) error {
channel<-arg
//更多逻辑在这里,如果出现问题可能返回错误
return nil
}
func main() {
IntChan := make(chan int)
StringChan := make(chan string)
SendToChannel(1,IntChan)
SendToChannel("hello",StringChan)
}
然而,这会导致编译错误,因为正如Kelsnare指出的那样,chan int
、chan string
和chan interface{}
是完全不同的类型。
看起来在2022年之前可能无法实现这个功能,除非泛型功能推出,但如果有人发现了解决方法,我很愿意听听。
英文:
I would like to write a function in go that takes a generic channel as an input. I'd like it to be able to accept any channel regardless of what type that channel is for. I assumed I could us func myFunc(channel chan interface{})
but I'm getting a compilation error when I try to pass my channel to it. Is there a way to do this in go?
---EDIT---
I've been asked for a minimal working example, so I thought I'd share some code below. What I would like is something along the lines of
package main
func SendToChannel(arg interface{}, channel chan interface{}) error {
channel<-arg
//more logic here, potentially returning errors if something goes wrong
return nil
}
func main() {
IntChan := make(chan int)
StringChan := make(chan string)
SendToChannel(1,IntChan)
SendToChannel("hello",StringChan)
}
However, this gives a compilation error because, as Kelsnare pointed out, chan int
, chan string
and chan interface{}
are three completely different types.
It seems likely that this will not be possible to achieve until generics come out in 2022, but if anyone has discovered a workaround I'd love to hear it.
答案1
得分: 3
chan interface{}
和chan int
是两个不同的类型定义。
考虑以下示例:
package main
import (
"fmt"
)
func myFunc(ch chan interface{}) {
fmt.Println("I'm called")
}
func main() {
ch := make(chan int)
myFunc(ch)
fmt.Println("Done")
}
这个函数会引发以下错误:
cannot use ch (type chan int) as type chan interface {} in argument to myFunc
让我们将类型更改为chan interface{}
:
package main
import (
"fmt"
)
func myFunc(ch chan interface{}) {
fmt.Println("I'm called")
}
func main() {
ch := make(chan interface{})
myFunc(ch)
fmt.Println("Done")
}
这个版本可以编译通过。
chan int
只是一个示例 - 除了将ch
的类型设置为chan interface{}
之外,任何其他类型都会导致错误。
如果你确实希望编写一个可以接受任何类型的chan int
、chan string
、chan customtype
的函数,目前唯一的选择是接受一个普通的interface{}
类型参数。
正如@mkopriva提到的,泛型即将到来。
英文:
chan interface{}
and chan int
are two distinct type definitions.
Consider this example:
package main
import (
"fmt"
)
func myFunc(ch chan interface{}) {
fmt.Println("I'm called")
}
func main() {
ch := make(chan int)
myFunc(ch)
fmt.Println("Done")
}
This function panics with
cannot use ch (type chan int) as type chan interface {} in argument to myFunc
Let's change the type to chan interface{}
package main
import (
"fmt"
)
func myFunc(ch chan interface{}) {
fmt.Println("I'm called")
}
func main() {
ch := make(chan interface{})
myFunc(ch)
fmt.Println("Done")
}
This compiles.
chan int
is used as an example - anything else apart from chan interface{}
as the type for ch
will result in a panic.
If you absolutely want a function that you want to be able to call with any of chan int
, chan string
, chan customtype
your only bet right now is to accept a plain interface{}
.
As @mkopriva mentioned though, generics are coming soon.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论