2014-01-22 45 views
0

我試圖調用此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 

我在做什麼錯?

+0

錯誤是哪一行? – Thomas

+0

更新了,是一個複製和粘貼。 – Lee

回答

2
class.ClassType = *obj.(*entities.ClassType) 
+0

謝謝!我花了幾個小時在這些該死的指針上停留。這裏是什麼,爲什麼類型斷言需要一個指針? – Lee

+0

它不,它需要取消引用來獲取值而不是內存地址。 –