下面由教學欄目給大家介紹go-carbon 1.2.0 版本,希望對需要的朋友有所幫助!
carbon 是一個輕量級、語意化、對開發者友好的Golang時間處理庫,支援鏈式呼叫和gorm、xrom等主流orm
如果您覺得不錯,請給個star吧
github:github.com/golang-module/carbon
gitee:gitee.com/go-package/carbon
// 使用github庫 go get -u github.com/golang-module/carbon import ( "github.com/golang-module/carbon") // 使用gitee庫 go get -u gitee.com/go-package/carbon import ( "gitee.com/go-package/carbon")
假設資料表為users,欄位有id(int)、name(varchar)、age(int)、graduated_at(date)、birthday(date)、created_at(datetime)、updated_at(datetime)、deleted_at(datetime)
type UserModel struct { ID int64 `json:"id"` Name string `json:"name"` Age int `json:"age"` Birthday carbon.Carbon `json:"birthday"` CreatedAt carbon.ToDateTimeString `json:"created_at"` DeletedAt carbon.ToTimestamp `json:"deleted_at"` GraduatedAt carbon.ToDateString `json:"graduated_at"` UpdatedAt carbon.ToTimeString `json:"updated_at"`}
user := UserModel { Name: "勾國印", Age: 18, Birthday: carbon.Now().SubYears(18), CreatedAt: carbon.ToDateTimeString{carbon.Now()}, DeletedAt: carbon.ToTimestamp{carbon.Parse("2020-08-05 13:14:15")}, GraduatedAt: carbon.ToDateString{carbon.Parse("2012-09-09")}, UpdatedAt: carbon.ToTimeString{carbon.Now()},}
user.ID // 18 user.Name // 勾國印 user.Birthday.ToDateString() // 2012-08-05 user.CreatedAt.ToTimestamp() // 2012-08-05 13:14:15 user.DeletedAt.ToDateTimeString() // 1596604455 user.GraduatedAt.AddDay().ToDateString() // 2012-09-10 user.UpdatedAt.ToDateString() // 2012-08-05
data, _ := json.Marshal(&user) fmt.Print(string(data)) // 輸出 { "id": 42, "name": "勾國印", "age": 18, "birthday": "2012-08-05 00:00:00", "created_at": "2020-08-05 13:14:15", "deleted_at": 1596604455 "graduated_at": "2012-09-09", "updated_at": "13:14:15",}
以上就是go-carbon1.2.0釋出了!完善優化對ORM的多場景支援的詳細內容,更多請關注TW511.COM其它相關文章!