-
更新完懵逼了,一堆NewDateTime方法报错 |
Beta Was this translation helpful? Give feedback.
Answered by
kuafuRace
Mar 25, 2025
Replies: 3 comments 2 replies
-
go中 v3 和 v2 会认为是不同的包 |
Beta Was this translation helpful? Give feedback.
0 replies
-
carbon.NewDateTime(c) -> carbon.NewLayoutType[carbon.DateTime](c) 或者 carbon.NewDateTime(c) -> carbon.NewFormatType[carbon.DateTime](c) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fnoopv
-
本次更新可以更灵活的自定义输出格式,只要实现接口方法就可以,比如 type RFC3339Layout struct {}
func (t CustomerLayout) SetLayout() string {
return carbon.RFC3339Layout
}
type ISO8601Format struct {}
func (t CustomerFormat) SetFormat() string {
return carbon.ISO8601Format
}
type User struct {
Customer1 carbon.LayoutType[RFC3339Layout] `json:"customer1"`
Customer2 carbon.FormatType[ISO8601Format] `json:"customer2"`
}
var user User
c := carbon.Parse("2020-08-05 13:14:15")
user.Customer1 = carbon.NewLayoutType[RFC3339Layout](c)
user.Customer2 = carbon.NewFormatType[ISO8601Format](c)
data, err := json.Marshal(&user)
if err != nil {
// 错误处理
log.Fatal(err)
}
fmt.Printf("%s", data)
// 输出
{"customer1":"2020-08-05T13:14:15Z","customer2":"2020-08-05T13:14:15+00:00"}
var person User
err := json.Unmarshal(data, &person)
if err != nil {
// 错误处理
log.Fatal(err)
}
fmt.Printf("%+v", person)
// 输出
{Customer1:2020-08-05T13:14:15Z Customer2:2020-08-05T13:14:15+00:00} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
或者