timezones.go 710 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Output:
  2. // Eastern Standard time
  3. // Universal Greenwich time
  4. // Central Standard time
  5. package main
  6. import "fmt"
  7. type TZ int
  8. const (
  9. HOUR TZ = 60 * 60
  10. UTC TZ = 0 * HOUR
  11. EST TZ = -5 * HOUR
  12. CST TZ = -6 * HOUR
  13. )
  14. var timeZones = map[TZ]string{UTC: "Universal Greenwich time",
  15. EST: "Eastern Standard time",
  16. CST: "Central Standard time"}
  17. func (tz TZ) String() string { // Method on TZ (not ptr)
  18. for name, zone := range timeZones {
  19. if tz == name {
  20. return zone
  21. }
  22. }
  23. return ""
  24. }
  25. func main() {
  26. fmt.Println(EST) // Print* knows about method String() of type TZ
  27. fmt.Println(0 * HOUR)
  28. fmt.Println(-6 * HOUR)
  29. }
  30. /* Output:
  31. Eastern Standard time
  32. Universal Greenwich time
  33. Central Standard time
  34. */