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")
資料庫支援
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的多場景支持的詳細內容。更多資訊請關注PHP中文網其他相關文章!