访问切片 Golang 模板中的结构中的特定字段
Access specific field in struct which is in slice Golang templates
我有这样的地方我发送到模板年数据(这是结构的一部分)。如何仅访问第二项(结构)的模板端 AcaYear
字段?整个第二项我可以访问为 {{index . 1 }}
。我还可以遍历 slice 并获取所有项目的 AcaYear
字段。但是只需要第二项的AcaYear
字段(结果应该是2021-2022
)。
package main
import (
"log"
"os"
"text/template"
)
type course struct {
Number string
Name string
Units string
}
type semester struct {
Term string
Courses []course
}
type year struct {
AcaYear string
Fall semester
Spring semester
Summer semester
}
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseFiles("tpl.gohtml"))
}
func main() {
years := []year{
year{
AcaYear: "2020-2021",
Fall: semester{
Term: "Fall",
Courses: []course{
course{"CSCI-40", "Introduction to Programming in Go", "4"},
course{"CSCI-130", "Introduction to Web Programming with Go", "4"},
course{"CSCI-140", "Mobile Apps Using Go", "4"},
},
},
Spring: semester{
Term: "Spring",
Courses: []course{
course{"CSCI-50", "Advanced Go", "5"},
course{"CSCI-190", "Advanced Web Programming with Go", "5"},
course{"CSCI-191", "Advanced Mobile Apps With Go", "5"},
},
},
},
year{
AcaYear: "2021-2022",
Fall: semester{
Term: "Fall",
Courses: []course{
course{"CSCI-40", "Introduction to Programming in Go", "4"},
course{"CSCI-130", "Introduction to Web Programming with Go", "4"},
course{"CSCI-140", "Mobile Apps Using Go", "4"},
},
},
Spring: semester{
Term: "Spring",
Courses: []course{
course{"CSCI-50", "Advanced Go", "5"},
course{"CSCI-190", "Advanced Web Programming with Go", "5"},
course{"CSCI-191", "Advanced Mobile Apps With Go", "5"},
},
},
},
}
err := tpl.Execute(os.Stdout, years)
if err != nil {
log.Fatalln(err)
}
}
只需将 index
调用分组并在结果上应用 .AcaYear
字段选择器:
{{ (index . 1).AcaYear }}
这将输出(在 Go Playground 上尝试):
2021-2022
我有这样的地方我发送到模板年数据(这是结构的一部分)。如何仅访问第二项(结构)的模板端 AcaYear
字段?整个第二项我可以访问为 {{index . 1 }}
。我还可以遍历 slice 并获取所有项目的 AcaYear
字段。但是只需要第二项的AcaYear
字段(结果应该是2021-2022
)。
package main
import (
"log"
"os"
"text/template"
)
type course struct {
Number string
Name string
Units string
}
type semester struct {
Term string
Courses []course
}
type year struct {
AcaYear string
Fall semester
Spring semester
Summer semester
}
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseFiles("tpl.gohtml"))
}
func main() {
years := []year{
year{
AcaYear: "2020-2021",
Fall: semester{
Term: "Fall",
Courses: []course{
course{"CSCI-40", "Introduction to Programming in Go", "4"},
course{"CSCI-130", "Introduction to Web Programming with Go", "4"},
course{"CSCI-140", "Mobile Apps Using Go", "4"},
},
},
Spring: semester{
Term: "Spring",
Courses: []course{
course{"CSCI-50", "Advanced Go", "5"},
course{"CSCI-190", "Advanced Web Programming with Go", "5"},
course{"CSCI-191", "Advanced Mobile Apps With Go", "5"},
},
},
},
year{
AcaYear: "2021-2022",
Fall: semester{
Term: "Fall",
Courses: []course{
course{"CSCI-40", "Introduction to Programming in Go", "4"},
course{"CSCI-130", "Introduction to Web Programming with Go", "4"},
course{"CSCI-140", "Mobile Apps Using Go", "4"},
},
},
Spring: semester{
Term: "Spring",
Courses: []course{
course{"CSCI-50", "Advanced Go", "5"},
course{"CSCI-190", "Advanced Web Programming with Go", "5"},
course{"CSCI-191", "Advanced Mobile Apps With Go", "5"},
},
},
},
}
err := tpl.Execute(os.Stdout, years)
if err != nil {
log.Fatalln(err)
}
}
只需将 index
调用分组并在结果上应用 .AcaYear
字段选择器:
{{ (index . 1).AcaYear }}
这将输出(在 Go Playground 上尝试):
2021-2022