duan2891 2017-07-05 19:25
浏览 61
已采纳

在HTML文件中循环切片值

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

type blogs struct {
    id             int
    title          string
    featured_image string
    created_at     string
}

and created variable inside "xyz" function :

blog := blogs{}
blogData := []blogs{}

with value as :

    rows, err := db.Query("SELECT id, title, featured_image, created_at from blogs order by created_at desc limit 0,6")
    if err != nil {
        ctx.Application().Logger().Fatalf("MySQL Error fetching row %s
", err)
    }        
    for rows.Next() {
        rcan := rows.Scan(&id, &title, &featured_image, &created_at)

        blog.id = id
        blog.title = title
        blog.featured_image = featured_image
        blog.created_at = created_at

        blogData = append(blogData, blog)
    }

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

  <ul>
  {{ range $value := .blogData }}
    <li>{{ $value.title }}</li>
  {{ end }}
  </ul>

Error :

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

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

{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条回答 默认 最新

  • dongmangji0950 2017-07-05 19:37
    关注

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

    type blogs struct {
        ID             int
        Title          string
        Featured_image string
        Created_at     string
    }
    

    Read more about Exported/Unexported Identifiers In Go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计