Skip to content

Instantly share code, notes, and snippets.

@bcho
Last active August 29, 2015 14:07
Show Gist options
  • Save bcho/511168ace7cbcc150903 to your computer and use it in GitHub Desktop.
Save bcho/511168ace7cbcc150903 to your computer and use it in GitHub Desktop.
trick
package main
import "fmt"
type Year uint
type Month uint
func (year Year) isLeap() bool {
return ((year%4 == 0) && (year%100 != 0)) || (year%400 == 0)
}
func (year Year) days() uint {
if year.isLeap() {
return 366
} else {
return 365
}
}
func (month Month) days() uint {
return []uint{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}[month]
}
func main() {
fmt.Printf("year 2014 has %d days.\n", Year(3).days())
fmt.Printf("month 11 has %d days.\n", Month(11).days())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment