
go template 如何赋值变量?
go 中通过变量赋值来填充模板内容,这与 php 的 assign 方法类似。
当执行模板时,通过 execute 函数的第二个参数传入变量。该参数可以是 map 或 struct,包含模板中使用的变量。
对于您的示例代码,可以如下方式赋值变量:
import (
"text/template"
"os"
)
type filename struct {
name string
}
func main() {
// 模板内容
const templatetext = `
-
{{range .filelist}}
- {{.name}} {{end}}
结果如下:
- a.txt
- b.txt










