stu
stu

Reputation: 175

Should you use protobuf as the datatype you use for processing or only for transmission?

I have a matrix struct written in Go. That matrix struct has a bunch of methods. I want to be able to efficiently compute matrix operations but I also want to be able to send it over the wire in order to distribute the computation.

I currently have the matrix and its methods separate from the protobuf definition. When I need to send it over the wire I have to create a new pb.Matrix{} from the existing Matrix{} struct and then make my grpc call. That seems like a waste. So, is it a waste? And should I just be defining my matrix struct as a protobuf definition and then use embedding to define operations on it? Or is it better to keep them separate from each other?

Upvotes: 1

Views: 294

Answers (1)

Ainar-G
Ainar-G

Reputation: 36239

In terms of architecture, I'd keep them separate. That would agree with the Single Responsibility Principle. In one of my projects we use this form:

type Foo struct { ... }

func NewFooFromProto(f *myproto.Foo) *Foo { ... }

func (f *Foo) ToProto() *myproto.Foo { ... }

Upvotes: 1

Related Questions