daytime, _ := time.Parse("2006-01-02", day)
var weekday = daytime.Weekday()
fmt.Println(weekday, int(daytime.Weekday()), daytime.Weekday())
Output : Friday 5 Friday
感觉输出好诡异,明明是一个Int类型,为何不强转的情况下是个string形式的输出。
Weekday的类型源码定义
type Weekday int
const (
Sunday Weekday = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
time中的Weekday()方法源码
// Weekday returns the day of the week specified by t.
func (t Time) Weekday() Weekday {
return absWeekday(t.abs())
}
// absWeekday is like Weekday but operates on an absolute time.
func absWeekday(abs uint64) Weekday {
// January 1 of the absolute year, like January 1 of 2001, was a Monday.
sec := (abs + uint64(Monday)*secondsPerDay) % secondsPerWeek
return Weekday(int(sec) / secondsPerDay)
}
真感觉好诡异,始终是ToYoung😳