I defined 3 Message types using protobuf. (MsgA, MsgB, MsgC)
Message MsgA {
string content;
int64 A;
};
Message MsgB {
string content;
char B;
};
Message MsgC {
string content;
double C;
};
and I defined a MsgType to indicate the message is MsgA/MsgB/MsgC
Message MsgType {
string type; // indicate MsgA/ MsgB/ MsgC
};
Then, I generated some messages and stored in a memory map file in this format:
|MsgType|MsgA/MsgB/MsgC|some end marker|
when I read from the buffer, I want to do something similar to this:
msgType := &MsgType{}
err := proto.Unmarshal(byteArrayforMsgType, msgType)
...
switch msgType.GetType() {
case "MsgA":
a := &MsgA{}
err := prto.Unmarshal(byteArrayforMsg, a)
...
case "MsgB":
b := &MsgB{}
err := prto.Unmarshal(byteArrayforMsg, b)
...
case "MsgC":
c := &MsgC{}
err := prto.Unmarshal(byteArrayforMsg, c)
...
}
Here is the question: Since each case is quite similar, I want to do something similar to C++
#define CASE(MsgType)\
case #MsgType:\
msg := createObject<msgType>();\
...
switch type {
CASE(MsgA);
CASE(MsgB);
CASE(MsgC);
}
Actually there are many message types, not just A,B,C. There will be repeating codes in each case section. Is there any method in Go to do the similar thing as C++?