英文:
Golang assigning struct to another struct field not working
问题
我正在尝试使用Gorp获取所有健身课程。健身课程有一个课程类型,所以我运行第二个查询来检索它们。我成功获取了所有的课程类型,但是最后的赋值部分出了问题。
package entities
import (
"fmt"
"github.com/coopernurse/gorp"
"time"
)
type Class struct {
Id int
ClassTypeId int
ClassType ClassType
VideoPath string
VideoSize int
Duration float64
CreatedAt time.Time
VisibleAt time.Time
NoLongerVisibleAt time.Time
}
func LatestClasses(dbmap *gorp.DbMap) *[]Class {
var classes []Class
query := "SELECT * FROM Class"
_, err := dbmap.Select(&classes, query)
if err != nil {
panic(err)
}
for _, class := range classes {
classTypeForClass(dbmap, &class)
}
return &classes
}
func classTypeForClass(dbmap *gorp.DbMap, class *Class) {
var classType ClassType
query := "SELECT * FROM ClassType "
query += "WHERE Id=?"
err := dbmap.SelectOne(&classType, query, class.ClassTypeId)
if err != nil {
panic(err)
}
fmt.Println(classType) <<<<<<< Lists Yoga, Pilates etc.
class.ClassType = classType <<<<<<< Seemingly does nothing
}
更新:
ClassType结构如下:
package entities
import (
"time"
)
type ClassType struct {
Id int
Code string
Name string
InstructorId int
CreatedAt time.Time
}
另一个更新:
我将数据显示如下:
<h1>
Latest Classes
</h1>
<hr/>
{{ range .}}
{{.VideoPath}}
<br>
{{.ClassType.Name}}
<br>
{{.VideoSize}}
<br>
{{.Duration}}
<br>
{{.CreatedAt}}
{{.NoLongerVisibleAt}}
<br><br>
{{end}}
英文:
I am trying to use Gorp to get all gym classes. Gym classes have a class type so I run a second query to retrieve them. I get all the class types back, but the final assingment is not working for some reason.
package entities
import (
"fmt"
"github.com/coopernurse/gorp"
"time"
)
type Class struct {
Id int
ClassTypeId int
ClassType ClassType
VideoPath string
VideoSize int
Duration float64
CreatedAt time.Time
VisibleAt time.Time
NoLongerVisibleAt time.Time
}
func LatestClasses(dbmap *gorp.DbMap) *[]Class {
var classes []Class
query := "SELECT * FROM Class"
_, err := dbmap.Select(&classes, query)
if err != nil {
panic(err)
}
for _, class := range classes {
classTypeForClass(dbmap, &class)
}
return &classes
}
func classTypeForClass(dbmap *gorp.DbMap, class *Class) {
var classType ClassType
query := "SELECT * FROM ClassType "
query += "WHERE Id=?"
err := dbmap.SelectOne(&classType, query, class.ClassTypeId)
if err != nil {
panic(err)
}
fmt.Println(classType) <<<<<<<<<<< Lists Yoga, Pilates etc.
class.ClassType = classType <<<<<<<< Seemingly does nothing
}
UPDATE
The ClassType struct looks like this:
package entities
import (
"time"
)
type ClassType struct {
Id int
Code string
Name string
InstructorId int
CreatedAt time.Time
}
ANOTHER UPDATE
I display the data as follows:
<h1>
Latest Classes
</h1>
<hr/>
{{ range .}}
{{.VideoPath}}
<br>
{{.ClassType.Name}}
<br>
{{.VideoSize}}
<br>
{{.Duration}}
<br>
{{.CreatedAt}}
{{.NoLongerVisibleAt}}
<br><br>
{{end}}
答案1
得分: 2
这个任务正常工作,但你没有将其分配给正确的对象:
在你的循环中:
for _, class := range classes {
classTypeForClass(dbmap, &class)
}
class
是 classes
切片中元素的一个副本。你的 classTypeForClass(dbmap, &class)
将类分配给了这个副本,而副本在循环体结束时被丢弃。
尝试像这样修改:
for i := range classes {
classTypeForClass(dbmap, &(classes[i]))
}
或者更好的方式是:让 classTypeForClass
返回类,并直接分配给它:
for i := range classes {
classes[i].ClassType = classTypeForClass(dbmap, &(classes[i]))
}
(甚至可以将 class
作为值传递,而不是指针。)
英文:
The assignment works properly, but you are not assigning it to the proper object:
In your loop
> for _, class := range classes {
> classTypeForClass(dbmap, &class)
> }
class
is a copy of the elements in the classes
slice. Your classTypeForClass(dbmap, &class)
properly assigns the class to this copy and the copy is discarded at the end of the loop body.
Try something like
for i := range classes {
classTypeForClass(dbmap, &(classes[i]))
}
or maybe nicer: Have classTypeForClass return the class and just assign it like
for i := range classes {
classes[i].ClassType = classTypeForClass(dbmap, &(classes[i]))
}
(Maybe even passing class as a value, not as a pointer.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论