[英]Go - Idiomatic way around interface slice issue
I have a WorkDay struct holding data about the times someone worked, a WorkWeek struct to hold a bunch of WorkDays, and a WorkMonth struct to hold a bunch of WorkWeeks. 我有一个WorkDay结构,包含有关某人工作时间的数据,一个WorkWeek结构来保存一堆WorkDays,还有一个WorkMonth结构来保存一堆WorkWeek。 The idea is to have each return the total hours worked during that period. 我们的想法是让每个人返回在此期间的总工作时数。
type WorkDay struct {
StartTime time.Time
EndTime time.Time
}
type WorkWeek struct {
WorkDays []WorkDay
}
type WorkMonth struct {
WorkWeeks []WorkWeek
}
func (w WorkDay) HoursWorked() time.Duration {
// Find hours worked through simple subtraction.
}
func (w WorkWeek) HoursWorked() time.Duration {
var totalHours time.Duration
for _, day := range w.WorkDays {
totalHours += day.HoursWorked()
}
return totalHours
}
func (w WorkMonth) HoursWorked() time.Duration {
var totalHours time.Duration
for _, week := range w.WorkWeeks {
totalHours += week.HoursWorked()
}
return totalHours
}
This code works just fine, but the duplication in WorkWeek.HoursWorked()
and WorkMonth.HoursWorked()
really grinds my gears. 这段代码工作得很好,但是WorkWeek.HoursWorked()
和WorkMonth.HoursWorked()
的重复工作确实让我感觉很好。 I tried to do the following, thinking I was very clever: 我试着做以下事情,以为我很聪明:
func (w WorkWeek) HoursWorked() time.Duration {
return sumHoursWorked(w.WorkDays)
}
func (m WorkMonth) HoursWorked() time.Duration {
return sumHoursWorked(m.WorkWeeks)
}
type countable interface {
HoursWorked() time.Duration
}
func sumHoursWorked(timeFrames []countable) time.Duration {
var totalHours time.Duration
for _, frame := range timeFrames {
totalHours += frame.HoursWorked()
}
return totalHours
}
However, as explained here , even though WorkDay
implements countable
, a slice of WorkDay
s does not count as a slice of countable
s. 但是,正如这里所解释的那样,即使WorkDay
实现了countable
,一片WorkDay
也不算作countable
s片。
So, is there some nifty, idiomatic way out of this situation that I'm missing, or am I just stuck with the duplication? 那么,是否有一些漂亮的,惯用的方式摆脱了我所缺少的这种情况,或者我只是坚持复制?
No. Either have a slice of countables
which gives you dynamic method dispatch or some programming (your first solution) or restructure your types. 没有。要么有一些countables
,它们为您提供动态方法调度或一些编程(您的第一个解决方案)或重组您的类型。 I have no idea of your problem domain but months consisting of weeks consisting of days seems odd, at least the month/week stuff. 我不知道你的问题领域,但由几天组成的几个月似乎很奇怪,至少是月/周的东西。
No because slice of countables
is another type. 不,因为countables
片段是另一种类型。 You could define your own slice type and attach an Add method to it. 您可以定义自己的切片类型并为其附加Add方法。
func (w WorkWeek) HoursWorked() time.Duration {
return sumHoursWorked(w.WorkDays)
}
func (m WorkMonth) HoursWorked() time.Duration {
return sumHoursWorked(m.WorkWeeks)
}
type countable interface {
HoursWorked() time.Duration
}
type SliceCountable []countable
func (m *SliceCountable) Add( c countable ) {
*m = append(*m, c )
}
func (m SliceCountable) HoursWorked() time.Duration {
var totalHours time.Duration
for _, frame := range m {
totalHours += frame.HoursWorked()
}
return totalHours
}
func sumHoursWorked(timeFrames []countable) time.Duration {
var totalHours time.Duration
for _, frame := range timeFrames {
totalHours += frame.HoursWorked()
}
return totalHours
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.