我有一個WorkDay結構,它保存有關某人工作的時間的數據,一個WorkWeek結構以保存一堆WorkDays,以及一個WorkMonth結構來保存一堆WorkWeeks。這個想法是讓每個人都返回在此期間工作的總小時數。Go - 圍繞界面切片問題的習慣方式
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
}
此代碼工作得很好,但在WorkWeek.HoursWorked()
和WorkMonth.HoursWorked()
重複真正研我的齒輪。我試圖做到以下幾點,以爲我是很聰明:
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
}
然而,正如解釋here,即使WorkDay
實現countable
的WorkDay
秒的片不計入的countable
個切片。
那麼,是否有一些漂亮的,習慣性的方式擺脫這種情況,我錯過了,或者我只是堅持重複?
您如何處理財政月份包含經常不完整的財政週數可變的數量?看起來像WorkMonth直接包含[] WorkDay,並且有些方法在必要時提取星期,這將是一個更簡單的實現。 – krait
你是對的。這個計劃主要是爲了練習,但如果我要進一步研究它,我可能會重新思考我的類型,正如Volker在下面所建議的。 – sozorogami