我爲Golang使用Protobuf。 Protobuf生成類型指針實現的消息類型proto.Message()
。 例如將盒裝結構轉換爲盒裝指針 - golang
func (*SomeMessage) Message() {}
的protobuf的lib中有像Marshal(proto.Message)
現在我的實際問題的方法。
message := SomeMessage {}
SendMessage(&message)
func SendMessage(message interface{}) {
switch msg := message.(type) {
case proto.Message:
//send across the wire or whatever
default:
//non proto message, panic or whatever
}
}
上述工作正常。 但是,如果我不將消息作爲指針傳遞,那麼SendMessage中的代碼將不匹配,因爲接口只在SomeMessage指針上實現,而不是在值上實現。
我想要做的是:
message := SomeMessage {}
SendMessage(message) //pass by value
//there are more stuff going on in my real code, but just trying to show the relevant parts
func SendMessage(message interface{}) {
//match both pointer and value as proto.Message
//and then turn the value into a pointer so that
//other funcs or protobuf can consume it
message = MagicallyTurnBoxedValueIntoBoxedStruct(message)
switch msg := message.(type) {
case proto.Message:
//send across the wire or whatever
default:
//non proto message, panic or whatever
}
}
最好,我想能夠通過既作爲指針和值。 爲什麼我想按值傳遞的原因是,這可以作爲一個貧窮的芒隔離傳遞消息,當跨越夠程/線程等 (在缺乏不變性)
所有這一切,如果很可能避免採取行動protobuf生成器生成的允許值也被視爲proto.Message()
。 或者如果有更好的方法來做不可變的消息。
這不是超級重要的,如果可能的話,冷靜,如果它不是,MEH :-)
[編輯]
如果我有消息的reflect.Type和的reflect.Type消息的指針類型。 是否可以通過某種方式創建指向使用「反射」值的指針類型實例?
工作,謝謝! –
@RogerAlsing我想指出的是,雖然這樣做會很慢並且過度設計,不應該用於任何實際項目。 – OneOfOne
是的,我明白了,最好的解決方案是讓protoc生成不可變的結構。但是看到它是可能的,仍然很有趣。 –