Golang类型断言问题

Golang类型断言问题

问题描述:

I am trying to call this Gorp function http://godoc.org/github.com/coopernurse/gorp#DbMap.Get

I am doing this:

       // ClassType
    obj, err := c.Gorp.Get(entities.ClassType{}, class.ClassTypeCode)
    if err != nil {
        panic(err)
    }
    class.ClassType = obj.(*entities.ClassType)  <<<<<<<<< Error here

My Class looks like this:

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
}

I keep getting this error message: interface conversion: interface is *entities.ClassType, not entities.ClassType

If I change my code to :

            // ClassType
    obj, err := c.Gorp.Get(entities.ClassType{}, class.ClassTypeCode)
    if err != nil {
        panic(err)
    }
    class.ClassType = obj.(*entities.ClassType)

I then get this message:

cannot use obj.(*entities.ClassType) (type *entities.ClassType) as type entities.ClassType in assignment

What am I doing wrong?

class.ClassType = *obj.(*entities.ClassType)