我試圖調用此GORP功能http://godoc.org/github.com/coopernurse/gorp#DbMap.GetGolang類型斷言問題
我這樣做:
// ClassType
obj, err := c.Gorp.Get(entities.ClassType{}, class.ClassTypeCode)
if err != nil {
panic(err)
}
class.ClassType = obj.(*entities.ClassType) <<<<<<<<< Error here
我的班級是這樣的:
package entities
import (
"time"
)
type Class struct {
Id int
ClassTypeCode string
VideoPath string
VideoSize int
Duration float64
CreatedAt time.Time
VisibleAt time.Time
NoLongerVisibleAt time.Time
// Relationships
ClassType ClassType
Instructor User
Equipment []Equipment
}
我不斷收到此錯誤消息: 接口轉換:接口是* entities.ClassType,不是實體。類型
如果我改變我的代碼:
// ClassType
obj, err := c.Gorp.Get(entities.ClassType{}, class.ClassTypeCode)
if err != nil {
panic(err)
}
class.ClassType = obj.(*entities.ClassType)
然後我得到這個消息:
cannot use obj.(*entities.ClassType) (type *entities.ClassType) as type entities.ClassType in assignment
我在做什麼錯?
錯誤是哪一行? – Thomas
更新了,是一個複製和粘貼。 – Lee