2012-10-11 129 views
2

我需要在我的包的開始處設置一個變量,稍後將用parseFiles()填充,但我不知道如何設置該變量,因爲它不是一個字符串或int或任何泛型類似的變量。我如何設置變量,而不必在那裏添加一些任意名稱來設置它?golang變量設置

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") 

回答

4

你只需要用返回的類型替換= template.New("foo")。在這種情況下:

var templatefiles *template.Template // the return type of html/template.New() 

templatefiles現在是一個包含零指針類型template.Template全局變量。

+0

工作感謝!!!如果後來我想清除變量呢?基本上清空var templatefiles中的模板? –

+0

與任何指針一樣,您將其設置爲零。 'templatefiles = nil' –

+0

對不起,我錯了,它沒有工作。這是我得到的錯誤。 http://play.golang.org/p/a0irFLNgpo –