Created
May 22, 2014 07:26
-
-
Save sozorogami/5e16ef9744e17d94c73d to your computer and use it in GitHub Desktop.
問題は、一日、一週間、一ヶ月という期間で、働いた時間を保存して算出することです。「俺、頭いい!」と思いつつこんな実装にしたのですが、残念ながらWorkDayはcountableの内でも、[]WorkDayは[]countableとして通用しないよ?ってコンパイラに怒られるのです。sumHoursWorked内のコードを重複させないで実装する方法はありませんか。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type countable interface { | |
HoursWorked() time.Duration | |
} | |
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 { | |
return sumHoursWorked(w.WorkDays) | |
} | |
func (w WorkMonth) HoursWorked() time.Duration { | |
return sumHoursWorked(w.WorkWeeks) | |
} | |
func sumHoursWorked(timeFrames []countable) time.Duration { | |
var totalHours time.Duration | |
for i := range timeFrames { | |
totalHours += timeFrames[i].HoursWorked() | |
} | |
return totalHours | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[]WorkdDay
と[]countable
は別の型なので、そのまま引数で渡すことはできないようです。以下のようにすれば、コンパイルできます。似たような問題で悩んでいる人がいました。御参考までに。
https://groups.google.com/forum/#!topic/golang-nuts/Il-tO1xtAyE