1
我想我使用gorm
ORM库和gin
框架Golang抽象,以避免重复代码
基类
type Base struct {
Context *gin.Context // Passing gin request
}
func (b *Base) Add() {
err := b.Context.BindJSON(b)
if err != nil {
// handling error here
}
gorm.db.Create(b) // Here I am adding base data to database
}
子类来实现golang 抽象
type Shopper struct {
Base // Embedding Base struct
Name string,
Address string,
ContactNo int
}
处理程序
func handler (c *gin.Context) {
s := new(Shopper)
s.Context = c
s.Add() // Here I am expecting Add() method should bind JSON to shopper struct
// entry to database using gorm
}
Add()
方法没有采取其shopper
结构有任何属性。
在这里,我只是想避免在每个handler
其中 只是需要从JSON请求主体code duplication
和使用gorm
谢谢kostix –