doucaishi0077 2017-10-03 16:10
浏览 314
已采纳

如何在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 ?

  • 写回答

1条回答 默认 最新

  • dseve40868 2017-10-03 16:20
    关注

    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_

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部