mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 06:23:59 +08:00
37 lines
649 B
Go
Executable File
37 lines
649 B
Go
Executable File
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
|
|
*/
|