Răsfoiți Sursa

更新第十章timezones.go练习题答案 (#722)

* 根据map章节的教程,改写String方法

* format the source code

* update to make it more friendly to new learners.

* Update according to comments
SoniaChoo 6 ani în urmă
părinte
comite
f462e39fa6
1 a modificat fișierele cu 4 adăugiri și 5 ștergeri
  1. 4 5
      eBook/exercises/chapter_10/timezones.go

+ 4 - 5
eBook/exercises/chapter_10/timezones.go

@@ -15,15 +15,14 @@ const (
 	CST  TZ = -6 * HOUR
 )
 
-var timeZones = map[TZ]string{UTC: "Universal Greenwich time",
+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)
-	for name, zone := range timeZones {
-		if tz == name {
-			return zone
-		}
+	if zone, ok := timeZones[tz]; ok {
+		return zone
 	}
 	return ""
 }