首页 > 代码库 > Golang枚举示例之Day

Golang枚举示例之Day

GOPATH\src\day\day.go

package daytype Day uintconst (    Sunday Day = iota    Monday    Tuseday    Wednesday    Thursday    Friday    Saturday)func (day Day) String() string {    switch day {    case Sunday:        return "Sunday"    case Monday:        return "Monday"    case Tuseday:        return "Tuesday"    case Wednesday:        return "Wednesday"    case Friday:        return "Friday"    case Saturday:        return "Saturday"    default:        return "Invalid"    }}

 

main.go

package mainimport "fmt"import "day"func main() {    var today day.Day    today = day.Sunday    tomorrow := day.Monday    fmt.Println("today:    ", today)    fmt.Println("tomorrow: ", tomorrow)    fmt.Printf("type of today:    %T\n", today)    fmt.Printf("type of tomorrow: %T\n", tomorrow)}

 

输出:

 

Golang枚举示例之Day