go-carbon v2.3.1 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库

本文主要是介绍go-carbon v2.3.1 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。

目前已被 awesome-go 收录,如果您觉得不错,请给个 star 吧

github.com/golang-module/carbon

gitee.com/golang-module/carbon

安装使用
Golang 版本大于等于 1.16
// 使用 github 库
go get -u github.com/golang-module/carbon/v2import "github.com/golang-module/carbon/v2"// 使用 gitee 库
go get -u gitee.com/golang-module/carbon/v2import "gitee.com/golang-module/carbon/v2"
Golang 版本小于 1.16
// 使用 github 库
go get -u github.com/golang-module/carbonimport "github.com/golang-module/carbon"// 使用 gitee 库
go get -u gitee.com/golang-module/carbonimport  "gitee.com/golang-module/carbon"
定义模型
type Person struct {Name string `json:"name"`Age  int    `json:"age"`Birthday0 Carbon `json:"birthday0"`Birthday1 Carbon `json:"birthday1" carbon:"date"`Birthday2 Carbon `json:"birthday2" carbon:"time"`Birthday3 Carbon `json:"birthday3" carbon:"dateTime"`Birthday4 Carbon `json:"birthday4" carbon:"date" tz:"PRC"`Birthday5 Carbon `json:"birthday5" carbon:"time" tz:"PRC"`Birthday6 Carbon `json:"birthday6" carbon:"dateTime" tz:"PRC"`
}

type Person struct {Name string `json:"name"`Age  int    `json:"age"`Birthday0 Carbon `json:"birthday0"`Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02"`Birthday2 Carbon `json:"birthday2" carbon:"layout:15:04:05"`Birthday3 Carbon `json:"birthday3" carbon:"layout:2006-01-02 15:04:05"`Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02" tz:"PRC"`Birthday5 Carbon `json:"birthday5" carbon:"layout:15:04:05" tz:"PRC"`Birthday6 Carbon `json:"birthday6" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"`
}

type Person struct {Name string `json:"name"`Age  int    `json:"age"`Birthday0 Carbon `json:"birthday0"`Birthday1 Carbon `json:"birthday1" carbon:"format:Y-m-d"`Birthday2 Carbon `json:"birthday2" carbon:"format:H:i:s"`Birthday3 Carbon `json:"birthday3" carbon:"format:Y-m-d H:i:s"`Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d" tz:"PRC"`Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s" tz:"PRC"`Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s" tz:"PRC"`
}

如果 carbon 标签没有设置,默认是 layout:2006-01-02 15:04:05;如果 tz 标签没有设置,默认是 Local

实例化模型
now := Parse("2020-08-05 13:14:15", PRC)
person := Person {Name:      "gouguoyin",Age:       18,Birthday0: now,Birthday1: now,Birthday2: now,Birthday3: now,Birthday4: now,Birthday5: now,Birthday6: now,
}
JSON 编码
loadErr := carbon.LoadTag(&person)
if loadErr != nil {// 错误处理log.Fatal(loadErr)
}
data, marshalErr := json.Marshal(person)
if marshalErr != nil {// 错误处理log.Fatal(marshalErr)
}
fmt.Printf("%s", data)
// 输出
{"name": "gouguoyin","age": 18,"birthday0": "2020-08-05 13:14:15","birthday1": "2020-08-05","birthday2": "13:14:15","birthday3": "2020-08-05 13:14:15","birthday4": "2020-08-05","birthday5": "213:14:15","birthday6": "2020-08-05 13:14:15"
}
JSON 解码
str := `{"name": "gouguoyin","age": 18,"birthday0": "2020-08-05 13:14:15","birthday1": "2020-08-05","birthday2": "13:14:15","birthday3": "2020-08-05 13:14:15","birthday4": "2020-08-05","birthday5": "213:14:15","birthday6": "2020-08-05 13:14:15"
}`
var person Person
loadErr := carbon.LoadTag(&person)
if loadErr != nil {// 错误处理log.Fatal(loadErr)
}unmarshalErr := json.Unmarshal([]byte(str), &person)
if unmarshalErr != nil {// 错误处理log.Fatal(unmarshalErr)
}fmt.Sprintf("%s", person.Birthday0) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday1) // 2020-08-05
fmt.Sprintf("%s", person.Birthday2) // 13:14:15
fmt.Sprintf("%s", person.Birthday3) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday4) // 2002-08-05
fmt.Sprintf("%s", person.Birthday5) // 13:14:15
fmt.Sprintf("%s", person.Birthday6) // 2002-08-05 13:14:15
更新日志
  • 修复在 Now 方法中设置测试时间时 testNow 为 0,造成 IsSetTestNow 方法返回 false 的 bug
  • 添加性能测试文件 xxx_bench_test.go
  • 增加格式模板常量,如 DateTimeFormat, DateFormat, TimeFormat, AtomFormat, ANSICFormat
  • loadTag 函数中 carbon 标签增加对 datetimedatetimeiso8601 等字符串的支持
  • loadTag 函数中新增 tz 标签,用于设置时区
  • ParseByLayout 方法中添加对 UVXZ 格式化符号的支持
  • ToFormatStringFormat 方法中添加对 vux 格式符号的支持
  • ClearTestNow 方法重命名为 UnSetTestNow
  • HasTestNow 方法重命名为 IsSetTestNow
  • xxx_test.go 文件重命名为 xxx_unit_test.go
  • json.go 文件重命名为 encoding.go, json_test.go 文件重命名为 encoding_unit_test.go
  • ClosestFarthest 方法从 traveler.go 文件移动到 extremum.go,从 traveler_test.go 文件移动到 extremum_unit_test.go
  • SetTestNow 方法中接收者类型从 结构体 更改为 指针

这篇关于go-carbon v2.3.1 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/552212

相关文章

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

golang版本升级如何实现

《golang版本升级如何实现》:本文主要介绍golang版本升级如何实现问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录golanwww.chinasem.cng版本升级linux上golang版本升级删除golang旧版本安装golang最新版本总结gola

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体