已经和Go,Goroutines和Channels玩了几天了。
经过大量的练习和一些与Goroutines和错误处理(How to handle errors and terminate Goroutine using WaitGroup)有关的帮助,现在我遇到了一个我不知道如何解决的问题。
问题是我想创建一个Mapper,它的功能可以接受任何类型的chan
。我试图通过传递chan interface{}
而不是真实类型来解决我能想到的唯一方法,但这显然行不通。 (如下例所示)
因此,我正在寻找正确的方法,并确认这甚至是正确的方法。
package main
import (
"fmt"
"golang.org/x/sync/errgroup"
)
var g errgroup.Group
// ItemOne struct
type ItemOne struct {
N int
}
func main() {
m := &Mapper{}
itemsOne, err := mapItemOne(m)
if err != nil {
// Handle error...
}
fmt.Println(itemsOne)
itemsTwo, err := mapItemTwo(m)
if err != nil {
// Handle error...
}
fmt.Println(itemsOne)
}
func mapItemOne(m *Mapper) ([]*ItemOne, error){
c := make(chan *ItemOne, 10)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
g.Go(func() error {
return m.mapItems(c, data)
})
mappedItems := make([]*ItemOne, 0)
for elem := range c {
mappedItems = append(mappedItems, elem)
}
err := g.Wait()
if err != nil {
return nil, err
}
return mappedItems, nil
}
// ItemTwo struct
type ItemTwo struct {
N int
}
func mapItemTwo(m *Mapper) ([]*ItemTwo, error){
c := make(chan *ItemTwo, 10)
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
g.Go(func() error {
return m.mapItems(c, data)
})
mappedItems := make([]*ItemTwo, 0)
for elem := range c {
mappedItems = append(mappedItems, elem)
}
err := g.Wait()
if err != nil {
return nil, err
}
return mappedItems, nil
}
// Mapper struct
type Mapper struct {}
func (m *Mapper) mapItems(c chan *interface{}, data []int) error {
defer close(c)
// Imagine that something here can error...
for _, v := range data {
fmt.Println(v)
item := &ItemOne{
N: v,
}
c <- item
}
return nil
}
(例如,已注释掉ItemTwo代码的示例):
https://play.golang.org/p/PlqPflP7Yf7
无效的示例(已注释ItemTwo代码):
https://play.golang.org/p/xM89GVY2BoX
我在这里的操场上列举了两个例子。第一个有效,但第二个由于有效的ItemTwo代码而损坏。
希望有人可以在这里指出正确的方向。谢谢。
最佳答案
问题是我想创建一个Mapper,它的功能可以接受任何类型的chan。
您根本无法在Go的类型系统中做到这一点。您必须确定类型,如果类型是interface
,则必须使用该确切类型,并且所有函数签名都必须匹配。 Go中没有协方差。
最佳建议:停止尝试编写通用代码。这是可行的,但它丑陋,缓慢且复杂。
关于go - 如何在具有chan参数接受多个类型的结构上创建func?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59107134/