gpt4 book ai didi

go - Range 在正确显示所有内容时无法迭代 true 吗?

转载 作者:IT王子 更新时间:2023-10-29 02:09:05 27 4
gpt4 key购买 nike

我在使用 Go 模板时遇到了一个奇怪的问题。出于某种原因,当我使用双 range它停止在代码中呈现其下方的所有内容。

// Index.html
{{define "index"}}
{{range $k, $element := .Items}}
{{range $element}}
{{.Title}}
{{end}}
{{end}}
{{end}}

这是我的 Go 代码:

data := IndexData{
Items: items,
}

IndexTemplate := template.Must(template.New("skeleton.html").Funcs(FuncTemplate).ParseFiles("skeleton.html", "index.html"))
IndexTemplate.ExecuteTemplate(w, "skeleton", data)

它确实在我的页面上正确显示了数据并且没有错误。这里唯一的问题是它会在显示最后一个项目后停止页面呈现。

在我的框架中,我根据他们访问的页面显示我的模板:

// Skeleton.html
{{define "skeleton"}}
{{block "index".}}{{end}}
{{block "account.register".}}{{end}}
{{block "account.login".}}{{end}}
{{block "account.profile".}}{{end}}
{{end}}

为什么在显示范围内的最后一项后停止渲染?

编辑:

只显示错误 executing "index" at <$element>: range can't iterate over true

编辑 2:

.Itemmap[string]interface{}包含以下内容:

map[result:[map[Title:Hello World2 Content:Lorem ipsum dolor sit amet2...] map[Title:Hello World Content:Lorem ipsum dolor sit amet...]] success:true]

解决方案

我设法通过在没有 success:true 的情况下正确返回我需要使用的数据来解决这个问题部分以及将其用作 interface{}所以我不必使用 2 个范围循环。

最佳答案

外部范围使用键 resultsuccess 遍历映射。内部范围尝试迭代这些键的值。 success 的值为 true。不可能对 bool 进行取值。

仅更改 result 的模板范围:

{{define "index"}}
{{range .Items.result}}
{{.Title}}
{{end}}
{{end}}

此外,修改代码以检查和处理从 ExecuteTemplate 返回的错误。

关于go - Range 在正确显示所有内容时无法迭代 true 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52523136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com