<分区>
我们有以下功能:
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)
}
这一切现在都行不通了,因为据我所知,我只能“注册”消息的实例,而不是消息类型本身。因此,映射只会匹配消息的特定实例,而不是它的类型,而这正是我所需要的。
所以我想这需要重新设计。我可以完全放弃注册和 map 查找,但是
- 我无法将
Handle
函数更改为特定类型(签名将需要保留message interface{}
- 我想避免必须使用
reflect
,因为我很难与一些同事一起捍卫这样的解决方案。