簡體   English   中英

Golang類型斷言問題

[英]Golang type assertion issue

我試圖稱之為Gorp函數http://godoc.org/github.com/coopernurse/gorp#DbMap.Get

我這樣做:

       // 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,而不是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

我究竟做錯了什么?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM