Home >Backend Development >Golang >go-carbon1.2.0 is released! Improve and optimize multi-scenario support for ORM
The following is the tutorial column of golang to introduce the go-carbon 1.2.0 version to you, I hope it will be helpful to friends in need!
carbon is a lightweight, semantic, developer-friendly Golang time processing library that supports chain calls and mainstream ORMs such as gorm and xrom
If you think it is good, please give it a 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")
Assume that the data table is users, and the fields are 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",}
The above is the detailed content of go-carbon1.2.0 is released! Improve and optimize multi-scenario support for ORM. For more information, please follow other related articles on the PHP Chinese website!