登录
首页 >  Golang >  Go问答

泛型的切片基本地图类型支持

来源:stackoverflow

时间:2024-03-02 09:09:25 325浏览 收藏

一分耕耘,一分收获!既然都打开这篇《泛型的切片基本地图类型支持》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

有没有办法创建一个通用函数,可以在传递映射或切片类型与基本类型时调整其操作?

目标

创建一个具有灵活返回类型的切片读取函数生成器:

func valuereader[t ](i int) func ([]protoconvertable) t {
    return func (row []protoconvertable) t {
        return ...
    }
}

row := []protoconvertable{
    &data[int]{value: 333},
    &listdata{values: []protoconvertable{
        &data[string]{value: "hello"},
        &data[string]{value: "world"},
    }},
    &mapdata{values: map[protoconvertable]protoconvertable{
        &data[int]{value: 22}: &data[string]{value: "world"},
        &data[int]{value: 11}: &data[string]{value: "hello"},
    }},
}

datareader := valuereader[int](0) // a function that converts the first element to an int
listdatareader := valuereader[[]string](1) // a function that converts the second element to a slice
mapdatareader := valuereader[map[int]string](2) // a function that converts the third element to a map

data := datareader(row) // 333
listdata := listdatareader(row) // []string{"hello", "world"}
mapdata := mapdatareader(row) // map[int]string{11: "hello", 22: "world"}

类型

type valuetype interface {
    int | string
}

type protoconvertable interface {
    toproto() *pb.genericmessage
}

type data[t valuetype] struct {
    value t
}

func (d *data) toproto() *pb.genericmessage{
    ...
}

type listdata struct {
    values []protoconvertable
}

func (d *listdata) toproto() *pb.genericmessage {
    ...
}

type mapdata struct {
    values map[protoconvertable]protoconvertable
}

func (d *mapdata) toproto() *pb.genericmessage {
    ...
}

当前解决方案

func ValueReader[T ValueType](i int) func([]ProtoConvertable) T {
    return func(row []ProtoConvertable) T {
        return row[i].(*Data[T]).Value
    }
}

func ListValueReader[T ValueType](i int) func([]ProtoConvertable) []T {
    return func(row []ProtoConvertable) []T {
        vs := row[i].(*ListData).Values
        res := make([]T, len(vs))
        for i, v := range vs {
            res[i] = v.(*Data[T]).Value
        }
        return res
    }
}

func MapValueReader[K ValueType, V ValueType](i int) func([]ProtoConvertable) map[K]V {
    return func(row []ProtoConvertable) map[K]V {
        vs := row[i].(*MapData).Values
        res := make(map[K]V, len(vs))
        for k, v := range vs {
            res[k.(*Data[K]).Value] = v.(*Data[V]).Value
        }
        return res
    }
}

dataReader := ValueReader[int](0)
listDataReader := ListValueReader[string](1)
mapDataReader := MapValueReader[int, string](2)

注意:所有这些代码都是对更复杂的库的未经测试的简化。它可能需要一些调整才能实际工作。


正确答案


< 可能不存在。

主要问题是您想要对一个匹配基值和两个复合类型的类型参数进行建模,其中之一是您想要捕获 KV 的映射类型。

即使它存在,ValueReader 的主体也将是 T 上的类型开关,以返回每个专用读取器函数,因此涉及少量代码重复的现有解决方案总体上似乎是一个更好的策略。

我的建议是,当 T 的不同具体类型上的操作确实相同时,使用泛型。您可以阅读更多内容:https://go.dev/blog/when-generics

好了,本文到此结束,带大家了解了《泛型的切片基本地图类型支持》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

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