duan7664 2018-03-16 17:44
浏览 17
已采纳

有没有推荐的方法来定义切片集合的常见行为?

I am trying to get suggestion from the community in order to make the best practices. Please bear with me, with the following example:

Suppose that you work with half open intervals, that is, something that you know when it starts.

For example

  • There can be HalfOpenInterval restricted to a day. Example: you say "from 1:00 pm afterwards" (until the end of the day). Let's call it ClockInterval
  • There can be HalfOpenInterval restricted to the existence of universe. Example: you say "from 9 of july of 1810 we declare the indepedency" (until the end of the cosmos.. hypothetically). Let's call it Period

For both entities types: you work with a collection of them, so you usually have slices of clocks and periods in your code.

So now comes the problem: you must find the enclosing interval for a given time (func FindEnclosingHalfOpenInterval) for both clocks and periods, so you start writing the code...

And well, i get into this matter... how i should organize the code in order to write only once the common func. (func FindEnclosingHalfOpenInterval).

So i get into this code: https://play.golang.org/p/Cy7fFaFzYJR

But i keep wondering if there is a better way to define common behaviour for collection of slices.

Please reader you shall realize that i need to do an "element by element" conversion for each type of slice (and i have a type of slice for each type of concrete HalfOpenInterval i define). So i wonder if there is there any way that allows me to introduce new types of HalfOpenInterval without having to do some adjustement and "automatically" gets the abilitiy to use the func FindEnclosingHalfOpenInterval?. Perhaps my rich-oo-java-based mind is not the correct way to face the problems in the simplistic-straight-ahead-go-world. I'm all hears, to any suggestion.

  • 写回答

2条回答 默认 最新

  • dssjxvbv918586 2018-03-16 22:23
    关注

    The key Problem here is you need to convert a slice from one type to another type.

    Converting Slices

    The correct way to do this is to create a new slice and loop over it converting every single item. You can do this fast(er) if you create the array beforehand:

    func ToIntervalsFromClockIntervals(clockIntervals []ClockInterval) HalfOpenIntervals {
        intervals := make(HalfOpenIntervals, 0, len(clockIntervals))
        for _, clockInterval := range clockIntervals {
            intervals = append(intervals, clockInterval)
        }
        return intervals
    }
    

    Composition

    Apart from that composition is another way you could solve the problem that you want to write the GetEnclosingInterval function only once. I'm not saying it is better: it has other advantages and disadvantages. What fits better depends on how else you use the slices apart from what you posted here.

    Here my refactored suggestion (and fixed): https://play.golang.org/p/Ko43hJUMpyT (TehSṕhinX you forgot to make the mutable method baseIntervals.add with pointer recievers instead ofvalue recievers(or whatever is the name for non-pointer recievers))

    The HalfOpenIntervals does not exist any more. Instead you have two different types for CLockIntervals and PeriodIntervals and both have the sorting and GetEnclosingInterval function implemented via a common base struct.

    For convenience I added a Add function and a New... function for each of them. This is where the disadvantages come in: Since CLockIntervals (and PeriodIntervals) is not a slice any more but a struct you will need convenience functions to work with the inner slice from the outside.

    -- edit --

    Over generalising

    Coming from a oo-oriented background myself I know the drive to avoid duplicated code at all costs.

    Writing go code for more than 2 years now on a full time basis I learned that this is not always the best approach in go. Duplicating code in go for different types is a common thing I do these days. Not sure if all would agree with this statement, though.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看