我需要在我的包的开头设置一个变量,稍后将用parseFiles()填充,但我不知道如何设置变量,因为它不是字符串或int或类似的泛型.我如何设置变量而不必在那里添加一些任意名称来设置它?
var templatefiles = template.New("foo") // Im having to do New("foo") just to set the variable
// Later on adding files to original variable
templatefiles.New("template name").Parse("Template text here")
Run Code Online (Sandbox Code Playgroud)
您只需要替换= template.New("foo")返回的类型.在这种情况下:
var templatefiles *template.Template // the return type of html/template.New()
Run Code Online (Sandbox Code Playgroud)
templatefiles现在是一个全局变量,包含指向类型的nil指针template.Template.