carbon 是一個輕量級、語義化、對開發者友好的Golang時間處理庫,支持鏈式調用和gorm、xrom等主流ormgit
若是您以爲不錯,請給個star吧github
github:github.com/golang-module/carbongolang
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)json
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",}