希望这是我与指针有关的最后一个问题:
我正在调用存储库方法来获取一部分健身房课程。我在这样的视图中显示它们:
{{ range .}}
{{.VideoPath}} << Correct
<br>
{{.Instructor.Email}} << Blank
<br>
{{.ClassType.ClassTypeCode}} << Blank
{{end}}
教师和类类型字段作为空结构出现,但在 ClassRepository 中我做了一些 Printlns 并打印了正确的数据。某处存在指针问题或其他问题。我做错了什么?
这是存储库:
package repositories
type ClassRepository struct {
Gorp gorp.SqlExecutor
}
func (c ClassRepository) ClassesForLastNDays(days int) []entities.Class {
var classes []entities.Class
// Gets the classes - omitted for brevity
c.populateClassRelationships(classes)
return classes
}
func (c ClassRepository) populateClassRelationships(classes []entities.Class) {
for i := range classes {
class := classes[i]
// ClassType
obj, err := c.Gorp.Get(entities.ClassType{}, class.ClassTypeCode)
if err != nil {
panic(err)
}
class.ClassType = *obj.(*entities.ClassType)
fmt.Println(class.ClassType) << Returns correct data
// Instructor and Equipment
Same for instructor and Equipment
}
}
更新:
经过大量的 printlns,我可以确认问题出在 populateClassRelationships 之后,填充的值都丢失了。
func (c ClassRepository) ClassesForLastNDays(days int) []entities.Class {
var classes []entities.Class
// Gets the classes - omitted for brevity
c.populateClassRelationships(classes) <<<< In the method,Println has values
return classes <<<< Values have been lost
}
精慕HU
相关分类