登录
首页 >  Golang >  Go问答

创建一个能够接受多种类型 chan 参数的函数在一个结构中

来源:stackoverflow

时间:2024-03-04 19:21:26 367浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《创建一个能够接受多种类型 chan 参数的函数在一个结构中》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

这几天一直在研究 go、goroutines 和 channels。

经过大量练习和一些与 goroutine 和错误处理相关的帮助(如何使用 waitgroup 处理错误并终止 goroutine),我现在遇到了一个我不知道如何解决的问题。 p>

问题是我想创建一个映射器,它有一个接受任何类型的 chan 的函数。我尝试用我能想到的唯一方法来解决这个问题,即传入 chan 接口{} 而不是真实类型,但这显然不起作用。 (如下例所示)

因此,我正在寻找执行此操作的正确方法,并确认这甚至是正确的方法。

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 中不存在协变/逆变。

最佳建议:停止尝试编写通用代码。这是可行的,但它很丑陋、缓慢且复杂。

本篇关于《创建一个能够接受多种类型 chan 参数的函数在一个结构中》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>