我有這樣的地方我發送到模板年數據(這是結構的切片)。如何AcaYear僅訪問第二項(結構)的模板側字段?整個第二項我可以訪問為{{index . 1 }}. 我還可以在切片上進行范圍并獲取AcaYear所有項目的字段。但只需要AcaYear第二項的字段(結果應該是2021-2022)。package mainimport ( "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.Templatefunc 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"}, }, },
訪問切片 Golang 模板中的結構中的特定字段
慕工程0101907
2022-05-23 16:41:26