2023 大语言模型技术报告.pdf
宙飒天下网 carbon 是一个轻量级、语义化、对开发者友好的 Golang 时间处理库,支持链式调用、农历和 gorm、xorm 等主流 orm。
目前已被 awesome-go-cn 收录,如果您觉得不错,请给个 star 吧
github.com/golang-module/carbon
gitee.com/golang-module/carbon
定义模型
type Person struct { Name string `json:"name"` Age int `json:"age"` Birthday1 Carbon `json:"birthday1"` Birthday2 Carbon `json:"birthday2" carbon:"date" tz:"PRC"` Birthday3 Carbon `json:"birthday3" carbon:"time" tz:"PRC"` Birthday4 Carbon `json:"birthday4" carbon:"dateTime" tz:"PRC"` Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` }
或
type Person struct { Name string `json:"name"` Age int `json:"age"` Birthday1 Carbon `json:"birthday1"` Birthday2 Carbon `json:"birthday2" carbon:"layout:2006-01-02" tz:"PRC"` Birthday3 Carbon `json:"birthday3" carbon:"layout:15:04:05" tz:"PRC"` Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"` Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` }
或
type Person struct { Name string `json:"name"` Age int `json:"age"` Birthday1 Carbon `json:"birthday1"` Birthday2 Carbon `json:"birthday2" carbon:"format:Y-m-d" tz:"PRC"` Birthday3 Carbon `json:"birthday3" carbon:"format:H:i:s" tz:"PRC"` Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d H:i:s" tz:"PRC"` Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"` Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"` Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"` Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"` }
实例化模型
now := Parse("2020-08-05 13:14:15", PRC) person := Person { Name: "gouguoyin", Age: 18, Birthday1: now, Birthday2: now, Birthday3: now, Birthday4: now, Birthday5: now, Birthday6: now, Birthday7: now, Birthday8: 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, "birthday1": "2020-08-05 13:14:15", "birthday2": "2020-08-05", "birthday3": "13:14:15", "birthday4": "2020-08-05 13:14:15", "birthday5": 1596604455, "birthday6": 1596604455999, "birthday7": 1596604455999999, "birthday8": 1596604455999999999 }
Json 解码
str := `{ "name": "gouguoyin", "age": 18, "birthday1": "2020-08-05 13:14:15", "birthday2": "2020-08-05", "birthday3": "13:14:15", "birthday4": "2020-08-05 13:14:15", "birthday5": 1596604455, "birthday6": 1596604455999, "birthday7": 1596604455999999, "birthday8": 1596604455999999999 }` 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.Birthday1) // 2002-08-05 13:14:15 fmt.Sprintf("%s", person.Birthday2) // 2020-08-05 fmt.Sprintf("%s", person.Birthday3) // 13:14:15 fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 13:14:15 fmt.Sprintf("%d", person.Birthday5) // 1596604455 fmt.Sprintf("%d", person.Birthday6) // 1596604455999 fmt.Sprintf("%d", person.Birthday7) // 1596604455999999 fmt.Sprintf("%d", person.Birthday8) // 1596604455999999999
更新日志
还没有评论,来说两句吧...