我想在 golang 上写类似 CRUD 的东西。我看到像
type CRUD interface {
Save(entity interface{})() // done
Update(entity interface{})() // done
Delete(entity interface{})() // done
All() []interface{} // problem is here
}
我有几个模型结构。
type User struct {
Login string
Password string
}
type Comment struct {
UserId int64
Message string
CreatedAt int64
}
我有一些服务:
// Struct should implement interface CRUD and use instead of interface{} User struct
type UserService struct {
Txn SomeStructForContext
}
func (rec *UserService) Save(entity interface{}) {
user := entity.(*model.User)
// TODO operation with user
}
// All the same with Update and Delete
func (rec *UserService) All() ([]interface{}) {
// TODO: I can't convert User struct array for return
}
我希望,它会解释什么问题
不负相思意
相关分类