英文:
Should you use protobuf as the datatype you use for processing or only for transmission?
问题
我有一个用Go语言编写的矩阵结构体。这个矩阵结构体有很多方法。我想要能够高效地计算矩阵操作,同时也想要能够通过网络发送它以便进行分布式计算。
目前,我将矩阵及其方法与protobuf定义分开。当我需要通过网络发送时,我必须从现有的Matrix{}
结构体创建一个新的pb.Matrix{}
,然后进行grpc调用。这似乎有些浪费。所以,这是一种浪费吗?我应该将我的矩阵结构体定义为protobuf定义,然后使用嵌入来定义其操作吗?还是最好将它们分开保持独立?
英文:
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?
答案1
得分: 1
从架构的角度来看,我会将它们分开。这符合单一职责原则。在我的一个项目中,我们使用以下形式:
type Foo struct { ... }
func NewFooFromProto(f *myproto.Foo) *Foo { ... }
func (f *Foo) ToProto() *myproto.Foo { ... }
英文:
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 { ... }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论