我想编写一个函数,可以将某些字段添加到Firebase消息 struct 中.有两种不同类型的消息,MessageMulticastMessage,它们都包含AndroidAPNS个相同类型的字段,但消息类型彼此之间没有显式声明的关系.

我想我应该能够做到这一点:

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

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

但它给出的误差为message.Android undefined (type T has no field or method Android).我也不会写switch m := message.(type)(cannot use type switch on type parameter value message (variable of type T constrained by firebaseMessage)).

我可以写switch m := any(message).(type),但我仍然不确定这是否能满足我的要求.

我还发现了一些被联合和类型约束弄糊涂的人提出的其他SO问题,但我看不到任何有助于解释为什么这不起作用的答案(可能是因为我试图将其用于 struct 而不是接口?)或者联合类型约束的实际用途.

推荐答案

在Go 1.18中,您不能访问类型参数的公共字段,也不能访问类型参数的公共方法.这些功能无法正常工作,因为它们在该语言中尚不可用.如链接线程所示,常见的解决方案是为接口约束指定方法.

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

Solution 1: type switch

如果联合体中有少量类型,那么这种方法可以很好地工作.

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

Solution 2: wrapper with method

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

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

Solution 3: reflection

如果您有许多 struct ,那么最好使用反射.在这种情况下,并不严格需要类型参数,但有助于提供额外的类型安全性.请注意, struct 和字段必须为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?

Go相关问答推荐

Go 1.22 net/http群组路由

使用恶意软件 scanner (ClamAV)时的Google云存储桶上传文件验证

困扰围棋官方巡回赛的S建议所有方法都使用同一类型的接收器

如何在围棋中从多部分.Part中获取多部分.文件而不保存到磁盘?

无法使用Segentio;S Kafka-Go连接到融合的Kafka

重新赋值变量时未清除动态类型-这是错误吗?

是否可以在调试期间在 VSCode 中预览 github.com/shopspring/decimal 值?

GitHub 中 REST API 的 aws 凭证问题

创建新对象后如何返回嵌套实体?

Go http.FileServer 给出意外的 404 错误

通过环境变量配置 OTLP 导出器

如何修改go gin的默认端口?我的 8080 端口正在使用中

我相信我正确地在 sRGB 和线性 RGB 之间进行了转换,那么为什么深色的结果看起来更糟呢?

Golang 工作池实现意外工作

Golang Echo Labstack 如何在模板视图中调用函数/方法

HTTP 重定向不呈现新页面

Golang API 的 HTTPS

golangci-lint 的 GitHub 操作失败,无法加载 fmt

如何从应用程序调用谷歌云身份 API

如何访问Go 1.18泛型 struct (structs)中的共享字段