Golang 字符串转数字_字符串转uint_字符串转时间格式

2019-02-28 15:57 评论 0 条 超级小编
Golang 字符串转数字_字符串转uint_字符串转时间格式
//StrToInt string 转int
func StrToInt(str string) int {
    i, e := strconv.Atoi(str)
    if e != nil {
        return 0
    }
    return i
}

//StrToUInt string 转int
func StrToUInt(str string) uint {
    i, e := strconv.Atoi(str)
    if e != nil {
        return 0
    }
    return uint(i)
}

// StrToTime 字符串转time
func StrToTime(s string) time.Time {
    loc, _ := time.LoadLocation(define.TimeZone)
    timeLayout := "2006-01-02 15:04:05"
    t, _ := time.ParseInLocation(timeLayout, s, loc)
    return t
}

// StrToDate 字符串转time
func StrToDate(s string) time.Time {
    loc, _ := time.LoadLocation(define.TimeZone)
    timeLayout := "2006-01-02"
    t, _ := time.ParseInLocation(timeLayout, s, loc)
    return t
}

//StrToTimePtr ..
func StrToTimePtr(str string) *time.Time {
    loc, err := time.LoadLocation(define.TimeZone)
    if err != nil {
        return nil
    }
    t, e := time.ParseInLocation("2006-01-02 15:04:05", str, loc)
    if e != nil {
        return nil
    }
    return &t
}

版权声明:本文著作权归原作者所有,如有侵权,请联系我们立即删除!
来源:手动创建 分类:Golang 标签: