我遇到了将星期几字符串转换为time.Weekday值的问题。我找不到time包中内置的任何东西。
然后我写了这个简单的函数(满足我的需要):
var daysOfWeek = [...]string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
}
func parseWeekday(v string) (time.Weekday, error) {
for i := range daysOfWeek {
if daysOfWeek[i] == v {
return time.Weekday(i), nil
}
}
return time.Sunday, fmt.Errorf("invalid weekday '%s'", v)
}
在 Golang 中是否有任何其他推荐或更惯用的方法来执行此转换?
谢谢!
Helenr
翻翻过去那场雪
相关分类