登录
首页 >  Golang >  Go问答

使用通用函数处理不同结构中的通用成员

来源:stackoverflow

时间:2024-02-19 17:51:24 456浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《使用通用函数处理不同结构中的通用成员》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我想编写一个函数,可以将某些字段添加到 firebase 消息结构中。消息有两种不同类型:messagemulticastmessage,它们都包含相同类型的 androidapns 字段,但消息类型之间没有显式声明的关系。

我认为我应该能够做到这一点:

type firebaseMessage interface {
    *messaging.Message | *messaging.MulticastMessage
}

func highPriority[T firebaseMessage](message T) T {
    message.Android = &messaging.AndroidConfig{...}
    ....
    return message
}

但它给出了错误message.android未定义(类型t没有字段或方法android)。而且我也不能写 switch m := message.(type) 不能在类型参数值消息上使用类型开关(受 firebasemessage 约束的 t 类型变量))。

我可以写 switch m := any(message).(type),但我仍然不确定这是否能达到我想要的效果。

我发现了一些对联合和类型约束感到困惑的人提出的其他问题,但我看不到任何有助于解释为什么这不起作用的答案(也许是因为我试图将它与结构一起使用而不是接口?)或者联合类型约束实际上有什么用处。


正确答案


在 go 1.18 中,您无法访问类型参数的公共字段1,也无法访问公共方法2。这些功能之所以无法使用,只是因为它们尚未在该语言中提供。如链接线程所示,常见的解决方案是为接口约束指定方法。

但是,类型 *messaging.message*messaging.multicastmessage 没有通用的访问器方法,并且是在您无法控制的库包中声明的。

解决方案1:类型切换

如果联合中的类型数量较少,则此方法效果很好。

func highpriority[t firebasemessage](message t) t {
    switch m := any(message).(type) {
    case *messaging.message:
        setconfig(m.android)
    case *messaging.multicastmessage:
        setconfig(m.android)
    }
    return message
}

func setconfig(cfg *messaging.androidconfig) {
    // just assuming the config is always non-nil
    *cfg = &messaging.androidconfig{}
}

演示:https://go.dev/play/p/9iG0eSep6Qo

解决方案 2:使用方法进行包装

这归结为 How to add new methods to an existing type in Go?,然后将该方法添加到约束中。如果您有很多结构,它仍然不太理想,但代码生成可能会有所帮助:

type wrappedmessage interface {
    *messagewrapper | *multicastmessagewrapper
    setconfig(c foo.config)
}

type messagewrapper struct {
    messaging.message
}

func (w *messagewrapper) setconfig(cfg messaging.android) {
    *w.android = cfg
}

// same for multicastmessagewrapper

func highpriority[t wrappedmessage](message t) t {
    // now you can call this common method 
    message.setconfig(messaging.android{"some-value"})
    return message
}

演示:https://go.dev/play/p/JUHp9Fu27Yt

解决方案3:反射

如果您有很多结构,那么使用反射可能会更好。在这种情况下,类型参数并不是严格需要的,但有助于提供额外的类型安全性。请注意,结构和字段必须为 addressable 才能正常工作。

func highPriority[T firebaseMessage](message T) T {
    cfg := &messaging.Android{} 
    reflect.ValueOf(message).Elem().FieldByName("Android").Set(reflect.ValueOf(cfg))
    return message
}

演示:https://go.dev/play/p/3DbIADhiWdO

注释:

  1. How can I define a struct field in my interface as a type constraint (type T has no field or method)?
  2. In Go generics, how to use a common method for types in a union constraint?

好了,本文到此结束,带大家了解了《使用通用函数处理不同结构中的通用成员》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

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