Skip to content

Instantly share code, notes, and snippets.

@styoe
Created March 14, 2019 10:33
Show Gist options
  • Save styoe/eec07a2569472d697f77584ab106e9e4 to your computer and use it in GitHub Desktop.
Save styoe/eec07a2569472d697f77584ab106e9e4 to your computer and use it in GitHub Desktop.
protobuf example
// Proto messages
message TestMsg
{
bool param = 1;
}
// TEST wrapper
message TheInnocentNotification
{
bool is_everything_ok = 1;
}
message TheMessageOfDoom
{
bool destroy_everything = 1;
}
// The code
testMsg := &protoMsg.TestMsg{
Param: true,
}
testMsgBin, _ := testMsg.Marshal()
fmt.Printf("<%s>\n", fmtBits(testMsgBin))
innocentMsg := &protoMsg.TheInnocentNotification{}
proto.Unmarshal(testMsgBin, innocentMsg)
doomMsg := &protoMsg.TheMessageOfDoom{}
proto.Unmarshal(testMsgBin, doomMsg)
fmt.Println("innocentMsg", innocentMsg.GetIsEverythingOk()) // returns true
fmt.Println("doomMsg", doomMsg.GetDestroyEverything()) // returns true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment