mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
36
eBook/exercises/chapter_10/days.go
Executable file
36
eBook/exercises/chapter_10/days.go
Executable file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Day int
|
||||
|
||||
const (
|
||||
MO Day = iota
|
||||
TU
|
||||
WE
|
||||
TH
|
||||
FR
|
||||
SA
|
||||
SU
|
||||
)
|
||||
|
||||
var dayName = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
|
||||
|
||||
func (day Day) String() string {
|
||||
return dayName[day]
|
||||
}
|
||||
|
||||
func main() {
|
||||
var th Day = 3
|
||||
fmt.Printf("The 3rd day is: %s\n", th)
|
||||
// If index > 6: panic: runtime error: index out of range
|
||||
// but use the enumerated type to work with valid values:
|
||||
var day = SU;
|
||||
fmt.Println(day); // prints Sunday
|
||||
fmt.Println(0, MO, 1, TU)
|
||||
}
|
||||
/* Output:
|
||||
The 3rd day is: Thursday
|
||||
Sunday
|
||||
0 Monday 1 Tuesday
|
||||
*/
|
Reference in New Issue
Block a user