项目目录下新建一个models公共模块文件夹
创建文件
例:
package models
import "time"
/**
公共模块
*/
// 根据时间戳获取年月日时分秒
func UnixToTime(timestamp int) string {
nowtime := time.Unix(int64(timestamp), 0)
return nowtime.Format("2006-01-02 15:04:05")
}
// 获取时间戳
func GetUnix() int64 {
return time.Now().Unix()
}
// 日期转换成时间戳
func DateToUnix(str string) int64 {
temp := "2006-01-02 15:04:05"
t, err := time.ParseInLocation(temp, str, time.Local)
if err != nil {
return 0
}
return t.Unix()
}
// 获取当前年月日
func GetDay() string {
temp := "20060102"
return time.Now().Format(temp)
}
// 获取当前日期
func GetDate() string{
temp := "2006-01-02 15:04"
return time.Now().Format(temp)
}
//调用公共模块的方法
r.SetFuncMap(template.FuncMap{
"UnixToTime": models.UnixToTime,//调用models下面的方法
})
gin框架学习记录——Model自定义
最新推荐文章于 2023-09-18 05:00:00 发布