Rumah >pembangunan bahagian belakang >Golang >go-carbon1.2.0发布了!完善优化对ORM的多场景支持
下面由golang教程栏目给大家介绍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",}
Atas ialah kandungan terperinci go-carbon1.2.0发布了!完善优化对ORM的多场景支持. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!