dqlb38410 2016-01-15 07:12
浏览 30

将结构与其在golang中的json逻辑分开?

Is there an idiomatic way in go to separate a struct from its json marshal logic?

Normally:

package models

type Foo struct {
  Name  `json:"full_name"`
}

But I want a separation of concerns. I don't want the json specifying logic in the models package with the struct, maybe put the json logic in another serializers package. How would you do that in idiomatic go? Maybe similar to how rails projects handle active_model_serializers code

  • 写回答

1条回答 默认 最新

  • dongpan9760 2016-01-15 08:26
    关注

    It is arguable whether json tags belong to marshaling logic or to json model. I would say specifying json tags just describe the json model and as such it may be better residing next to your Go model.

    The marshaling / unmarshaling logic is implemented in the encoding/json package itself. If you need custom logic, you can specify / implement that by implementing the json.Marshaler and json.Unmarshaler interfaces. This means defining methods to your type. In Go you can only specify methods to types being in the same package, so if you would separate your model from your custom parsing logic, the parsing package could not define methods to model types. Spec: Method declarations:

    The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be declared in the same package as the method.

    That being said you would need to define your custom parsing logic on a different type, and you would need further logic to map / copy into the model type as part of the parsing logic. You would lose more than you would gain by separating the model from the parsing logic.

    Going further, the struct type you marshal into may contain unexported fields which - if the parsing logic is in the same package - can be initialized properly. By separating model and logic, the logic would have troubles initializing unexported fields. One way would be to provide exported methods or functions, but then those are exported to everyone else, not just for the parsing logic.

    I'd say the Go way and the easiest way is to put model and parsing logic into the same package. It still gives you a "small" separation possibility: you may put the type definition in one file (e.g. model.go), and you may put custom parsing logic (methods implementing json.Marshaler and json.Unmarshaler) in another file (e.g. parse.go) but in the same package of course, but it may be better to see a type and all its methods in one place (in one file).

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?