gpt4 book ai didi

Golang 类型断言问题

转载 作者:数据小太阳 更新时间:2023-10-29 03:07:40 27 4
gpt4 key购买 nike

我正在尝试调用这个 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

我的类(class)是这样的:

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
}

我不断收到此错误消息:接口(interface)转换:接口(interface)是*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)

关于Golang 类型断言问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294049/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com