在HTML文件中循环切片值

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

Looping slice value in HTML file

问题

我已经创建了一个结构体的切片和一个切片的数组。

  1. type blogs struct {
  2. id int
  3. title string
  4. featured_image string
  5. created_at string
  6. }

在"xyz"函数中创建了变量:

  1. blog := blogs{}
  2. blogData := []blogs{}

赋值如下:

  1. rows, err := db.Query("SELECT id, title, featured_image, created_at from blogs order by created_at desc limit 0,6")
  2. if err != nil {
  3. ctx.Application().Logger().Fatalf("MySQL Error fetching row %s\n", err)
  4. }
  5. for rows.Next() {
  6. rcan := rows.Scan(&id, &title, &featured_image, &created_at)
  7. blog.id = id
  8. blog.title = title
  9. blog.featured_image = featured_image
  10. blog.created_at = created_at
  11. blogData = append(blogData, blog)
  12. }

现在,我将"blogData"的值传递给"html"模板,并且在下面的"iterating"中出现错误:

  1. <ul>
  2. {{ range $value := .blogData }}
  3. <li>{{$value.title}}</li>
  4. {{ end }}
  5. </ul>

错误信息:

  1. template: master.html:18:5: executing "master.html" at <yield>: error calling yield: template: home.html:5:17: executing "home.html" at <$value.title>: title is an unexported field of struct type main.blogs

我该如何在模板中打印"title"和其他"blogData"变量的值?

如果我打印"$value",它会以以下格式返回所有的值:

  1. {5 This is Title img/blog.jpg 2017-07-05T10:11:30+05:30 }

但我想分别打印"title"、"featured_image"和其他日期。

非常感谢您的帮助。我正在使用"github.com/get-ion/ion"框架。

谢谢。

英文:

I have created slice of struct and an array of slice.

  1. type blogs struct {
  2. id int
  3. title string
  4. featured_image string
  5. created_at string
  6. }

and created variable inside "xyz" function :

  1. blog := blogs{}
  2. blogData := []blogs{}

with value as :

  1. rows, err := db.Query(&quot;SELECT id, title, featured_image, created_at from blogs order by created_at desc limit 0,6&quot;)
  2. if err != nil {
  3. ctx.Application().Logger().Fatalf(&quot;MySQL Error fetching row %s\n&quot;, err)
  4. }
  5. for rows.Next() {
  6. rcan := rows.Scan(&amp;id, &amp;title, &amp;featured_image, &amp;created_at)
  7. blog.id = id
  8. blog.title = title
  9. blog.featured_image = featured_image
  10. blog.created_at = created_at
  11. blogData = append(blogData, blog)
  12. }

and now, I have passed "blogData" value to "html" template and iterating below gives error :

  1. &lt;ul&gt;
  2. {{ range $value := .blogData }}
  3. &lt;li&gt;{{ $value.title }}&lt;/li&gt;
  4. {{ end }}
  5. &lt;/ul&gt;

Error :

  1. template: master.html:18:5: executing &quot;master.html&quot; at &lt;yield&gt;: error calling yield: template: home.html:5:17: executing &quot;home.html&quot; at &lt;$value.title&gt;: title is an unexported field of struct type main.blogs

How can I print "title" and other value from blogData Variable in my template.

if I print $value , it returns all value in below format

  1. {5 This is Title img/blog.jpg 2017-07-05T10:11:30+05:30 }

But I want to print, title , featured_image and other date separately.

Any help would be appreciated. I am using "github.com/get-ion/ion" framework

Thanks

答案1

得分: 1

要在模板上访问结构字段,它必须是导出字段。请将您的结构更新为:

  1. type blogs struct {
  2. ID int
  3. Title string
  4. Featured_image string
  5. Created_at string
  6. }

了解更多关于Go中的导出/未导出标识符的信息。

英文:

To access your struct field on template, it must be exported fields. Update your struct to-

  1. type blogs struct {
  2. ID int
  3. Title string
  4. Featured_image string
  5. Created_at string
  6. }

Read more about Exported/Unexported Identifiers In Go.

huangapple
  • 本文由 发表于 2017年7月6日 03:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/44934457.html
匿名

发表评论

匿名网友

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

确定