登录
首页 >  Golang >  Go问答

使用结构类型作为映射的键

来源:stackoverflow

时间:2024-03-21 16:54:30 405浏览 收藏

使用结构类型作为映射键的问题在于,只能“注册”消息实例,而无法“注册”消息类型本身。这导致地图只会匹配消息的特定实例,而不是其类型,从而使得在后处理器中快速查找消息类型是否需要后处理变得不可能。为了解决这个问题,本文提出了一种基于接口的方法,通过实现 Postprocess 接口来标记需要后处理的消息类型。该解决方案允许在后处理器中通过检查消息类型是否实现 Postprocess 接口来快速确定是否需要后处理。

问题内容

我们有以下功能:

func (h *handler) handle(message interface{}) error {
    //here there is a switch for different messages
    switch m := message.(type) {
    }
}

此签名已给出且无法更改。处理程序处理大约 20 种不同的消息类型。

现在,其中一些消息(大约 4 条)需要特殊的后处理。在不同的包中。

因此,我想这样做:

func (h *handler) handle(message interface{}) error {
        //here there is a switch for different messages

        switch m := message.(type) {
        }
        //only post-process if original message processing succeeds
        postprocessorpkg.process(message)
    }

现在,在 process 函数中,我想快速查找消息类型是否确实是我们需要后处理的类型。我不想在这里再做一次 switch 。有许多处理程序,位于不同的包中,具有不同数量的消息类型,并且它应该是通用的。

所以我正在考虑在后处理器中注册消息类型,然后进行查找:

func (p *postprocessor) register(msgtype interface{}) {
     registeredtypes[msgtype] = msgtype
}

然后

func (p *Postprocessor) Process(msgtype interface{}) error {
     if ok := registeredTypes[msgtype]; !ok {
        return errors.New("Unsupported message type")
     }
     prop := GetProp(registeredTypes[msgtype])
     doSmthWithProp(prop)
}

据我所知,现在这一切都不起作用,因为我只能“注册”消息的实例,而不能“注册”消息类型本身。因此,地图只会匹配消息的特定实例,而不是其类型,这正是我所需要的。

所以我想这需要重新设计。我可以完全放弃注册和地图查找,但是

  • 我无法将 handle 函数更改为特定类型(签名需要保留 message 接口{}
  • 我想避免必须使用 reflect,因为我很难与一些同事一起捍卫这样的解决方案。

解决方案


由于无法设置类型作为地图键,我最终决定实现以下解决方案,该解决方案基于@chrono kitsune的解决方案:

type Postprocess interface {
    NeedsPostprocess() bool
}

type MsgWithPostProcess struct {}

func (p *MsgWithPostProcess) NeedsPostprocess() bool {
  return true
}

type Msg1 struct {
   MsgWithPostProcess
   //other stuff
}

type Msg2 struct {
    MsgWithPostProcess
    //other stuff
}

type Msg3 struct {
    //no postprocessing needed
}

func (p *Postprocessor) Process(msgtype interface{}) error {
     if _, ok := msgtype.(Postprocess); ok {
        //do postprocessing
     }         
}

根据我所做的简单测试,只有 msg1msg2 会被后处理,而不是 msg3,这正是我想要的。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

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