douchunjing6587 2017-06-10 07:36
浏览 71
已采纳

golang继承来实现像集合/模型这样的主干

I am looking into golang as my new goto language for my web-backend. My current language there is PHP, where I implemented a datastructure resembling the Backbone Collection/Model structure.

excursus Collection / Model

This means I have a Collection containing multiple Models. Models have an attributes dictionary for storing the actual "public data". Collections have multiple convenience methods operating on the array of Models (sort, getByIndex, getByMatchingParameter, etc). Models also have their share of convenience methods.

In daily life there will be 90% of "things" that are represented the same way. For example I can store an Address the same way I store my User. Having an AddressesCollection containing my Addresses I can simply ask my AddressesCollection to give me all AddressModels where city=Berlin. And implementing this matching in my base class I can do the logically same thing on my UserCollection giving me all my Users which are deactivated by deactivated=true.

But sometimes (10%) I need a special method. getGeoData for Addresses for example. Now, in my oop-world, I can simply create a new class extending my BaseModel adding my method and use my extended class everywhere where my base class was used.

My "i simply don't get it"

How does this work in Go. I read a lot introductions into embedding / composition. But simply do not get my head around this.

This is where I stand, keep in mind this is my playground for learning the "inheritance" concept, not for having a production ready Collection / Model implementation.

GoPlayground

What I get is that I haven't "understood" how to write a "generic" method accepting not-quite-inherited types. I have the feeling that I have to use an interface there somewhere to get this "inheritance"/"extended class" feeling. But my current use of this is... well...

But maybe I am completely wrong in my concept of implementing my desired structure that way in go at all? Maybe there is a much more elegant way. Any hints greatly appreciated!

  • 写回答

2条回答 默认 最新

  • douyin2883 2017-06-19 13:29
    关注

    Ok, looks like I really didn't understand the go-way of doing this.

    The buzzwords that helped me to move on:

    • ORM
    • go generate

    Projects that look like solving my issue:

    Thanks for the answers, anyway!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog