golang经常使用的功能封装函数

//时间戳转换成年月日
func TimeTransDate() string {
   timeLayout := "2006-01-02 15:04:05"
   date_time := time.Unix(time.Now().Unix(), 0).Format(timeLayout)
   return date_time
}
//截取字符串 start 起点下标 end 终点下标(不包括)
func Substr(str string, start int, end int) (string,error) {
   rs := []rune(str)
   length := len(rs)

   if start < 0 || start > length {
      return "",errors.New("start is wrong:"+string(start))
   }

   if end < 0 || end > length {
      return "",errors.New("end is wrong:"+string(start))
   }

   return string(rs[start:end]),nil
}
// 生成32位MD5
func MD5(text string) string{
   ctx := md5.New()
   ctx.Write([]byte(text))
   return hex.EncodeToString(ctx.Sum(nil))
}

猜你喜欢

转载自blog.csdn.net/u010412301/article/details/84592753