Golang在Html/Template中处理数组迭代时遇到问题求教
Golang在Html/Template中处理数组迭代时遇到问题求教
我正在尝试使用 range 循环并访问多个数组的内容来构建 HTML 中的 div。
我通过独立的数组传递数据:
但这个方法不奏效,而且我找不到任何可用的示例来改进/测试它。
创建数据:
for _, relatedPost := range relatedPosts {
RelatedPostsTitles = append(RelatedPostsTitles, relatedPost.Title)
RelatedPostsImages = append(RelatedPostsImages, relatedPost.Image)
RelatedPostsSynopsies = append(RelatedPostsSynopsies, relatedPost.Synopsis)
RelatedPostsSlugs = append(RelatedPostsSlugs, relatedPost.Slug)
}
fmt.Printf("\n%+v\n", RelatedPostsSlugs)
// blog post
return c.Status(http.StatusOK).Render("post.html", fiber.Map{
"Title": post.Title + " - " + postID + " - afa7789 ",
"PostID": postID,
"PostDate": post.CreatedAt.Format("01-02-2006"),
"PostImage": post.Image,
"PostTitle": post.Title,
"PostContent": template.HTML(string(markdown.ToHTML([]byte(post.Content), nil, nil))),
"RPostsID": RelatedPostsIDs,
"RpostsSlugs": RelatedPostsSlugs,
"RPostsImages": RelatedPostsImages,
"RPostsTitles": RelatedPostsTitles,
"RPostsSynopsies": RelatedPostsSynopsies,
})
这是打印结果 [seven-deadly-sins tulio-is-fren]
然后我的 HTML 中有这段代码片段:
</style>
{{if .RPostsID}}
{{ $images := .RPostsImages }}
{{ $synp := .RPostsSynopsis }}
{{ $titles := .RPostsTitles }}
{{ $slugs := .RPostsSlugs }}
{{range $i, $id := .RPostsID}}
<div>
<p>bolinha?</p>
<a href="/blog/{{$id}}-{{index $slugs $i}}">
<p>{{index $titles $i}}</p>
<p>{{index $synp $i}}</p>
</a>
<hr class="horizontal_line_class" />
<div>
{{end}}
{{end}}
</section>
但我一直收到这个错误:
template: post.html:79:45: executing "post.html" at <index $slugs $i>: error calling index: index of untyped nil
更多关于Golang在Html/Template中处理数组迭代时遇到问题求教的实战教程也可以访问 https://www.itying.com/category-94-b0.html
afa7789:
RpostsSlugs
你的代码中是小写,而模板中是大写:
afa7789:
RPostsSlugs
更多关于Golang在Html/Template中处理数组迭代时遇到问题求教的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
问题出在模板变量 $slugs 被赋值为 nil。虽然你的 Go 代码中 RelatedPostsSlugs 数组有数据,但模板中 $slugs := .RPostsSlugs 这行可能因为字段名不匹配而获取到空值。
检查你的模板字段名是否完全匹配 Go 代码中的键名。在 Go 代码中你使用的是 "RpostsSlugs"(注意小写 p),但在模板中引用的是 .RPostsSlugs(大写 P)。这是大小写不匹配的问题。
修正 Go 代码中的键名:
return c.Status(http.StatusOK).Render("post.html", fiber.Map{
"Title": post.Title + " - " + postID + " - afa7789 ",
"PostID": postID,
"PostDate": post.CreatedAt.Format("01-02-2006"),
"PostImage": post.Image,
"PostTitle": post.Title,
"PostContent": template.HTML(string(markdown.ToHTML([]byte(post.Content), nil, nil))),
"RPostsID": RelatedPostsIDs,
"RPostsSlugs": RelatedPostsSlugs, // 修正为 RPostsSlugs
"RPostsImages": RelatedPostsImages,
"RPostsTitles": RelatedPostsTitles,
"RPostsSynopsies": RelatedPostsSynopsies,
})
同时,更简洁的解决方案是直接在模板中使用原始数据结构。与其传递多个独立的数组,不如传递结构体数组:
type RelatedPost struct {
ID string
Slug string
Image string
Title string
Synopsis string
}
// 在处理器中
var relatedPostsList []RelatedPost
for _, relatedPost := range relatedPosts {
relatedPostsList = append(relatedPostsList, RelatedPost{
ID: relatedPost.ID,
Slug: relatedPost.Slug,
Image: relatedPost.Image,
Title: relatedPost.Title,
Synopsis: relatedPost.Synopsis,
})
}
return c.Status(http.StatusOK).Render("post.html", fiber.Map{
// ... 其他字段
"RelatedPosts": relatedPostsList,
})
然后在模板中直接迭代:
{{if .RelatedPosts}}
{{range .RelatedPosts}}
<div>
<p>bolinha?</p>
<a href="/blog/{{.ID}}-{{.Slug}}">
<p>{{.Title}}</p>
<p>{{.Synopsis}}</p>
</a>
<hr class="horizontal_line_class" />
</div>
{{end}}
{{end}}
这种方法更清晰,避免了数组索引的复杂性,也减少了出错的可能性。

