Golang structs generated by protoc
can only copied by using a Clone()
method. Trying to
copy a struct mymsg = *protomsg
will result in an error as a lock will prevent the copy.
mkpbcopy
is a program that will generated a code that will allow for either a deep or shallow copy
of the entire protobuf.
usage: mkpbcopy -m <message> [-o <output>] <file>.pb.go
-m <message> Protobuf message to generate a copy function from
-o <output> Outpfile file. If not specified defaults to <message>.go
<file>.pb.go is the protoc generated output file for a protobuf
The generated go file is expected to be in the same directory as the original .pb.go, as it will export additional methods on the protobuf.
The two methods will will generated if <message.go> will be:
func (*Message) DeepCopy() *Message
DeepCopy will walk every element in Message
, making an copy of every element, recursively traversing
all elements contained by Message
. This is analagous to proto.Message.Clone()
but benchmarks
twice as fast.
DeepCopy
methods will exist for each sub message of Message
.
func (*Message) ShallowCopy() *Message
ShallowCopy
will only walk every native type in Message
making an copy of each element. Pointers will
remain unchanged.
Please feel free to submit issues, fork the repository and send pull requests! Feedback is always welcome.
When submitting an issue, please include an example that demonstrates the issue.