Html 如何跨越结构切片而不是切片结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24556001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:10:51  来源:igfitidea点击:

How to range over slice of structs instead of struct of slices

htmltemplatesgostructgo-templates

提问by Nicolas Marshall

Having played around with Go HTML templates a bit, all the examples I found for looping over objects in templates were passing structs of slices to the template, somewhat like in this example :

稍微玩过 Go HTML 模板后,我发现所有用于循环模板中对象的示例都将切片结构传递给模板,有点像这个例子:

type UserList struct {
    Id   []int
    Name []string
}

var templates = template.Must(template.ParseFiles("main.html"))

func rootHandler(w http.ResponseWriter, r *http.Request) {
    users := UserList{
        Id:   []int{0, 1, 2, 3, 4, 5, 6, 7},
        Name: []string{"user0", "user1", "user2", "user3", "user4"},
    }
    templates.ExecuteTemplate(w, "main", &users)
}

with the "main" template being :

“主要”模板为:

{{define "main"}}
    {{range .Name}}
        {{.}}
    {{end}}
{{end}}

This works, but i don't understand how I'm supposed to display each ID just next to its corresponding Name if i'm ranging on the .Name property only. I would find it more logical to treat each user as an object to group its properties when displaying.

这是有效的,但我不明白如果我只在 .Name 属性上范围内,我应该如何在其相应的名称旁边显示每个 ID。我会发现将每个用户视为一个对象以在显示时对其属性进行分组更合乎逻辑。

Thus my question:

因此我的问题:

What if I wanted to pass a slice of structs to the template?What would be the syntax to make this work? I haven't found or understood how to in the official html/template doc. I imagined something looking remotely like this:

如果我想将一段结构传递给模板怎么办?使这项工作的语法是什么?我还没有在官方的 html/template 文档中找到或理解如何使用。我想象的东西看起来像这样:

type User struct {
    Id   int
    Name string
}
type UserList []User
var myuserlist UserList = ...

and a template looking somewhat like this: (syntax here is deliberately wrong, it's just to get understood)

和一个看起来有点像这样的模板:(这里的语法是故意错误的,只是为了理解)

{{define "main"}}
    {{for each User from myuserlist as myuser}}
        {{myuser.Id}}
        {{myuser.Name}}
    {{end}}
{{end}}

回答by nvcnvn

Use:

用:

{{range .}}
    {{.Id}}
    {{.Name}}
{{end}}

for the template.
Here is a example: http://play.golang.org/p/A4BPJOcfpB
You need to read more about the "dot" in the package overview to see how to properly use this. http://golang.org/pkg/text/template/#pkg-overview(checkout the Pipelines part)

为模板。
下面是一个例子:http: //play.golang.org/p/A4BPJOcfpB
你需要阅读更多关于包概述中的“点”来了解如何正确使用它。http://golang.org/pkg/text/template/#pkg-overview查看管道部分)

回答by Jonathan Bérubé

I don't have the rep to comment, but to answer @ROMANIA_engineer, the source cited by elithrar has been retired, for anyone still looking for this reference :

我没有要评论的代表,但要回答@ROMANIA_engineer,elithrar 引用的来源已经退休,供仍在寻找此参考资料的任何人使用:

This book has been removed as it will shortly be published by APress. Please see Network Programming with Go: Essential Skills for Using and Securing Networks

这本书已被删除,因为它将很快由 APress 出版。请参阅使用 Go 进行网络编程:使用和保护网络的基本技能