Last active
August 29, 2015 14:07
-
-
Save bcho/511168ace7cbcc150903 to your computer and use it in GitHub Desktop.
trick
This file contains 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
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