Golang将结构体分配给另一个结构体字段不起作用。

huangapple go评论75阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;github.com/coopernurse/gorp&quot;
	&quot;time&quot;
)

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 := &quot;SELECT * FROM Class&quot;

	_, err := dbmap.Select(&amp;classes, query)
	if err != nil {
		panic(err)
	}

	for _, class := range classes {
		classTypeForClass(dbmap, &amp;class)
	}

	return &amp;classes
}

func classTypeForClass(dbmap *gorp.DbMap, class *Class) {
	var classType ClassType
	query := &quot;SELECT * FROM ClassType &quot;
	query += &quot;WHERE Id=?&quot;

	err := dbmap.SelectOne(&amp;classType, query, class.ClassTypeId)
	if err != nil {
		panic(err)
	}
	fmt.Println(classType) &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Lists Yoga, Pilates etc.
	class.ClassType = classType &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; Seemingly does nothing
}

UPDATE

The ClassType struct looks like this:

package entities

import (
	&quot;time&quot;
)

type ClassType struct {
	Id           int
	Code         string
	Name         string
	InstructorId int
	CreatedAt    time.Time
}

ANOTHER UPDATE

I display the data as follows:

&lt;h1&gt;
	Latest Classes
&lt;/h1&gt;
&lt;hr/&gt;


{{ range .}}
	{{.VideoPath}}
	&lt;br&gt;
	{{.ClassType.Name}}
	&lt;br&gt;
	{{.VideoSize}}
	&lt;br&gt;
	{{.Duration}}
	&lt;br&gt;
	{{.CreatedAt}}
	{{.NoLongerVisibleAt}}
&lt;br&gt;&lt;br&gt;
{{end}}

答案1

得分: 2

这个任务正常工作,但你没有将其分配给正确的对象:
在你的循环中:

for _, class := range classes {
    classTypeForClass(dbmap, &class)
}

classclasses 切片中元素的一个副本。你的 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, &amp;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, &amp;(classes[i]))
}

or maybe nicer: Have classTypeForClass return the class and just assign it like

for i := range classes {
    classes[i].ClassType = classTypeForClass(dbmap, &amp;(classes[i]))
}

(Maybe even passing class as a value, not as a pointer.)

huangapple
  • 本文由 发表于 2014年1月21日 17:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/21254381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定