问题描述
app / domain / repositories / class_repository.go在尝试覆盖切片指针时不断收到此错误。 :24:无法区分类(类型* [] entities.Class)
我做错了什么?
这是struct:
软件包储存库
pre>
导入(
mobifit / app / domain / entities
)
类型ClassRepository结构{
*存储库
}
func(c * ClassRepository)ClassesForLastNDays(days int)* [] entities.Class {
classes:= new([] entities.Class)
query:= Select(* )。
出自(班级)。
Where(VisibleAt> CURRENT_TIMESTAMP() - INTERVAL?DAY)。
OrderBy(ClassTypeId)。
Sql()
c.Repository.Select(类,查询,天)
c.populateClassRelationships(类)
返回类
}
func(c * ClassRepository)populateClassRelationships(classes * [] entities.Class){
for i:= range classes {<<<<<< <这里是问题
class:= classes [i]
// ClassType
c.Repository.GetById(class.ClassType,class.ClassTypeId)
//教师
c.Repository.GetById(class.Instructor,class.ClassType.InstructorId)
//设备
查询:=选择(E. *) )。
出自(设备E)。
加入(E.Id = CE.EquipmentId上的ClassEquipment CE)。
哪里(CE.ClassId =?)。
Sql()
c.Repository.Select(class.Equipment,query,class.Id)
}
}
以下是Class结构:
包实体
导入(
time
)
类型结构{
Id int
ClassTypeId int
VideoPath字符串
VideoSize int
持续时间float64
CreatedAt time.Time
VisibleAt time.Time
NoLongerVisibleAt time.Time
//关系
ClassType ClassType
教师用户
设备[]设备
}
解决方案您假设指向一个切片的指针会自动取消引用迭代。
情况并非如此,也没有原因是因为一个切片是ady是一种指针,呈现指向切片的指针完全没用。
来自进行更改的片段对调用者来说是可见的,类似于将
指针传递给底层数组。 slice由
- 指向底层数组中切片的第一个元素的指针
- 切片的长度
- 切片的容量(切片通常可以延长到数组的末尾)
这个结构非常小,使得指针无用。
I keep getting this error when trying to range over a slice pointer.
app/domain/repositories/class_repository.go:24: cannot range over classes (type *[]entities.Class)
What am I doing wrong?
Here is the struct:
package repositories import ( "mobifit/app/domain/entities" ) type ClassRepository struct { *Repository } func (c *ClassRepository) ClassesForLastNDays(days int) *[]entities.Class { classes := new([]entities.Class) query := Select("*"). From("Class"). Where("VisibleAt > CURRENT_TIMESTAMP() - INTERVAL ? DAY"). OrderBy("ClassTypeId"). Sql() c.Repository.Select(classes, query, days) c.populateClassRelationships(classes) return classes } func (c *ClassRepository) populateClassRelationships(classes *[]entities.Class) { for i := range classes { <<<<<<<<<<< Here is the problem class := classes[i] // ClassType c.Repository.GetById(class.ClassType, class.ClassTypeId) //Instructor c.Repository.GetById(class.Instructor, class.ClassType.InstructorId) // Equipment query := Select("E.*"). From("Equipment E"). Join("ClassEquipment CE on E.Id = CE.EquipmentId"). Where("CE.ClassId = ?"). Sql() c.Repository.Select(class.Equipment, query, class.Id) } }
Here is the Class struct:
package entities import ( "time" ) type Class struct { Id int ClassTypeId int VideoPath string VideoSize int Duration float64 CreatedAt time.Time VisibleAt time.Time NoLongerVisibleAt time.Time // Relationships ClassType ClassType Instructor User Equipment []Equipment }
解决方案You're assuming the pointer to a slice will be automatically dereferenced for the iteration.
That's not the case and there's no reason for that because a slice is already a kind of pointer, rendering a pointer to a slice totally useless.
From Effective Go :
Internally, a slice is made of
- a pointer to the first element of the slice in the underlying array
- the length of the slice
- the capacity of the slice (the slice can usually be extended until the end of the array)
This structure is very small, rendering a pointer useless.
这篇关于Golang无法覆盖指向切片的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!