你应该将protobuf用作处理数据类型还是仅用于传输?

huangapple go评论65阅读模式
英文:

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 { ... }

huangapple
  • 本文由 发表于 2017年4月8日 00:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/43282702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定