Skip to content

Instantly share code, notes, and snippets.

@evangwt
Created April 17, 2019 08:56
Show Gist options
  • Save evangwt/b371a7bdd7e1bee56ed89efd5654ba2d to your computer and use it in GitHub Desktop.
Save evangwt/b371a7bdd7e1bee56ed89efd5654ba2d to your computer and use it in GitHub Desktop.
package model
import (
"strings"
uuid "github.com/satori/go.uuid"
)
// BaseUUID auto gen uuid xorm base model
type BaseUUID struct {
Id string `xorm:"pk not null 'id'" json:"id"`
}
// BeforeInsert auto gen uuid while id is empty
func (b *BaseUUID) BeforeInsert() {
if len(b.Id) == 0 {
id, _ := uuid.NewV4()
b.Id = strings.Replace(id.String(), "-", "", -1)
}
}
// MyTable BaseUUID usage
type MyTable struct {
BaseUUID `xorm:"extends"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment