如何在Go中将数字转换为一个月

如何在Go中将数字转换为一个月

问题描述:

I just want to take an integer input and convert it into corresponding month using Go time package. Is there a way apart from defining months in using a const block and using iota to incrementally represent them ?

我只想获取一个整数输入,然后使用Go时间包将其转换为相应的月份。 除了使用const块定义月份并使用iota增量表示它们之外,还有其他方法吗? p> div>

You can use the type time.Month, which implements the Stringer interface, which means you can do something like:

m := time.Month(10)
fmt.Println(m) //"October" - could also do m.String() here
fmt.Println(int(m)) //10

https://play.golang.org/p/PeFfVZZIK_