我想在 golang 上寫類似 CRUD 的東西。我看到像type CRUD interface { Save(entity interface{})() // done Update(entity interface{})() // done Delete(entity interface{})() // done All() []interface{} // problem is here}我有幾個(gè)模型結(jié)構(gòu)。type User struct { Login string Password string}type Comment struct { UserId int64 Message string CreatedAt int64}我有一些服務(wù):// Struct should implement interface CRUD and use instead of interface{} User structtype UserService struct { Txn SomeStructForContext}func (rec *UserService) Save(entity interface{}) { user := entity.(*model.User) // TODO operation with user}// All the same with Update and Deletefunc (rec *UserService) All() ([]interface{}) { // TODO: I can't convert User struct array for return }我希望,它會(huì)解釋什么問(wèn)題
1 回答

不負(fù)相思意
TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
您正在嘗試轉(zhuǎn)換[]ConcreteType
為[]interface{}
,這并不隱式工作。
但是您可以轉(zhuǎn)換[]ConcreteType
為interface{}
然后將其轉(zhuǎn)換回[]ConcreteType.
- 1 回答
- 0 關(guān)注
- 309 瀏覽
添加回答
舉報(bào)
0/150
提交
取消