Integrating GORM uuid implementation using new binary(16) uuid implementation available in mysql version 8.
- Wraps
uuid.UUIDinside and converts into binary when needed. - Implements Gorm
ScanandValuefor data type conversion. - Implements
GormDataTypefor data type in sql. MarshalJSONandUnmarshalJSONimplementation for json encode/decode.
BeforeCreate hook for creating new uuids whenever new data has to be inserted.
// BeforeCreate ->
func (t *Test) BeforeCreate(tx *gorm.DB) error {
id, err := uuid.NewRandom()
t.ID = BinaryUUID(id)
return err
}model := Test{}
db.Find(&model, "id = ?", ParseUUID("ed67a4b2-8f77-4d18-8c58-0508e7b207e8"))
log.Printf("data: %+v\n", model)Will autogenerate uuid automatically because of BeforeCreate hook added
data := Test{Name: fmt.Sprintf("Time is: %s", time.Now())}
db.Create(&data)Check this article for more information