go-carbon 1.2.6 版本发布,优化错误处理机制,弃用出错直接 panic 的粗暴处理方式

栏目: 软件资讯 · 发布时间: 5年前

内容简介:carbon 是一个轻量级、语义化、对开发者友好的Golang时间处理库,支持链式调用、农历和gorm、xorm等主流orm 如果您觉得不错,请给个 star 吧 github:github.com/golang-module/carbon gitee:gitee.com/go-packag...

carbon 是一个轻量级、语义化、对开发者友好的Golang时间处理库,支持链式调用、农历和gorm、xorm等主流orm

如果您觉得不错,请给个 star 吧
github:github.com/golang-module/carbon
gitee:gitee.com/go-package/carbon

错误处理

如果有多个错误发生,只返回第一个错误信息,前一个错误排除后才返回下一个错误信息。建议使用 SetTimezone ()、Parse ()、ParseByFormat ()、ParseByDuration ()、AddDuration ()、SubDuration () 等方法时先进行错误处理判断,除非你能确保传入参数无误

场景一

c := carbon.SetTimezone(PRC).Parse("123456")
if c.Error != nil {
 // 错误处理... fmt.Println(c.Error)}
fmt.Println(c.ToDateTimeString())
// 输出
the value "123456" and layout "2006-01-02 15:04:05" don't match

场景二

c := carbon.SetTimezone("XXXX").Parse("2020-08-05")
if c.Error != nil {
 // 错误处理... fmt.Println(c.Error)}
fmt.Println(c.ToDateTimeString())
// 输出
invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for all valid timezone

场景三

c := carbon.SetTimezone("XXXX").Parse("12345678")
if c.Error != nil {
 // 错误处理... fmt.Println(c.Error)}
fmt.Println(c.ToDateTimeString())
// 输出
invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for all valid timezone

更新日志

  • 更新 readme.md 文档
  • 完善单元测试代码覆盖率
  • arbon 结构体的 Loc 私有属性改成 Loc 公开属性
  • carbon 结构体新增 Error 公开属性
  • 优化错误处理机制,弃用出错直接 panic 的粗暴处理方式
  • 新增 AddCenturies () 方法获取 N 世纪后时间
  • 新增 AddCentury () 方法获取一世纪后时间
  • 新增 SubCenturies () 方法获取 N 世纪前时间
  • 新增 SubCentury () 方法获取一世纪前时间
  • 新增 NextCenturies () 方法获取 N 世纪后时间 (不跨月)
  • 新增 NextCentury () 方法获取一世纪后时间 (不跨月)
  • 新增 PreCenturies () 方法获取 N 世纪前时间 (不跨月)
  • 新增 PreCentury () 方法获取一世纪前时间 (不跨月)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Big Java Late Objects

Big Java Late Objects

Horstmann, Cay S. / 2012-2 / 896.00元

The introductory programming course is difficult. Many students fail to succeed or have trouble in the course because they don't understand the material and do not practice programming sufficiently. ......一起来看看 《Big Java Late Objects》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具