go template 如何赋值变量
go 中的模板赋值方式与 php 不同。在 php 中,使用 assign() 方法将 php 变量赋值给模板。在 go 中,使用 execute() 函数的第二个参数将数据传到模板。
使用方法:
示例:
type FileName struct { Name string } func main() { fileList := []FileName{{"a.txt"}, {"b.txt"}} t, err := template.ParseFiles("html/show_dir_list.html") if err != nil { panic(err) } err = t.Execute(w, map[string]interface{}{"fileList": fileList}) if err != nil { panic(err) } }
在这个示例中:
注意:
以上就是Go Template 如何将数据传递给模板?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号