mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 22:06:51 +08:00
* 根据map章节的教程,改写String方法 * format the source code * update to make it more friendly to new learners. * Update according to comments
41 lines
688 B
Go
Executable File
41 lines
688 B
Go
Executable File
// Output:
|
|
// Eastern Standard time
|
|
// Universal Greenwich time
|
|
// Central Standard time
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
type TZ int
|
|
|
|
const (
|
|
HOUR TZ = 60 * 60
|
|
UTC TZ = 0 * HOUR
|
|
EST TZ = -5 * HOUR
|
|
CST TZ = -6 * HOUR
|
|
)
|
|
|
|
var timeZones = map[TZ]string{
|
|
UTC: "Universal Greenwich time",
|
|
EST: "Eastern Standard time",
|
|
CST: "Central Standard time"}
|
|
|
|
func (tz TZ) String() string { // Method on TZ (not ptr)
|
|
if zone, ok := timeZones[tz]; ok {
|
|
return zone
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println(EST) // Print* knows about method String() of type TZ
|
|
fmt.Println(0 * HOUR)
|
|
fmt.Println(-6 * HOUR)
|
|
}
|
|
|
|
/* Output:
|
|
Eastern Standard time
|
|
Universal Greenwich time
|
|
Central Standard time
|
|
*/
|