Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

go - Is there a recommended way to define common behaviour for collection of slices?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...