Golang无法在视图中显示检索到的数据。

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

Golang cannot display retrieved data in view

问题

希望这是我最后一个关于指针的问题:

我正在调用一个存储库方法来获取一组健身课程。我在视图中这样显示它们:

	{{ range .}}
			{{.VideoPath}} << 正确
			<br>
			{{.Instructor.Email}} << 空白
			<br>
			{{.ClassType.ClassTypeCode}} << 空白	    		
	{{end}}   

指导员和课程类型字段显示为空结构体,但是在ClassRepository中,我进行了一些Printlns,正确的数据被打印出来了。某处可能存在指针问题或其他问题。我做错了什么?

这是存储库的代码:

package repositories    
  
type ClassRepository struct {
	Gorp gorp.SqlExecutor
}

func (c ClassRepository) ClassesForLastNDays(days int) []entities.Class {
	var classes []entities.Class
	// 获取课程 - 省略部分代码
	c.populateClassRelationships(classes)
	return classes
}

func (c ClassRepository) populateClassRelationships(classes []entities.Class) {
	for i := range classes {
		class := classes[i]

		// 课程类型
		obj, err := c.Gorp.Get(entities.ClassType{}, class.ClassTypeCode)
		if err != nil {
			panic(err)
		}
		class.ClassType = *obj.(*entities.ClassType)
		fmt.Println(class.ClassType) << 返回正确的数据

		// 指导员和设备
		指导员和设备的代码类似


	}
}

更新:

经过大量的printlns,我可以确认问题出在populateClassRelationships之后,填充的值都丢失了。

func (c ClassRepository) ClassesForLastNDays(days int) []entities.Class {
    	var classes []entities.Class
    	// 获取课程 - 省略部分代码
    	c.populateClassRelationships(classes) <<<< 在该方法中,Println有值
    	return classes <<<< 值已丢失
    }
英文:

Hopefully this is my last question to do with pointers ever:

I am calling a repository method to get a slice of gym classes. I am displaying them in a view like this:

	{{ range .}}
			{{.VideoPath}} << Correct
			<br>
			{{.Instructor.Email}} << Blank
			<br>
			{{.ClassType.ClassTypeCode}} << Blank	    		
	{{end}}   

The instructor and classtype fields are coming up as empty structs, but in the ClassRepository I did some Printlns and the correct data gets printed. Somewhere there is a pointer issue or something. What have I done wrong?

Here is the repository:

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


	}
}

UPDATE:

After a whole lot of printlns I can confirm that the issue is here after populateClassRelationships the populated values are all lost.

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
    }

答案1

得分: 1

我相信问题出在你的函数populateClassRelationships上,它没有修改原始的数据结构,如下面的代码所示,最终输出的结果是:

[1]
[]

不起作用的代码:

func myFunc(data []int) {
    data = append(data, 1)
    fmt.Println(data)
}

func main() {
    d := []int { }
    myFunc(d)
    fmt.Println(d)
}

通过将参数作为指针引入,可以修改原始数据:

func myFuncNew(data *[]int) {
    *data = append(*data, 1)
    fmt.Println(*data)
}
英文:

I believe the issue is with your function populateClassRelationships where it is not modifying the original data structure as illustrated by the following code which finally outputs:

[1]
[]

Not working

func myFunc(data []int) {
    data = append(data, 1)
    fmt.Println(data)
}

func main() {
    d := []int { }
    myFunc(d)
    fmt.Println(d)
}

With the introduction of the parameters as a pointer the original data can be modified

func myFuncNew(data *[]int) {
    *data = append(*data, 1)
    fmt.Println(*data)
}

huangapple
  • 本文由 发表于 2014年1月23日 05:48:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/21294701.html
匿名

发表评论

匿名网友

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

确定