2012-05-02 34 views
82

例如,我想在一个源文件中同时使用text/template和html/template。 但下面的代码会抛出错误。如何在Go语言中导入和使用不同的同名软件包?

import (
    "fmt" 
    "net/http" 
    "text/template" // template redeclared as imported package name 
    "html/template" // template redeclared as imported package name 
) 

func handler_html(w http.ResponseWriter, r *http.Request) { 
    t_html, err := html.template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) 
    t_text, err := text.template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`) 

} 
+1

谢谢你提出这个问题。确定它在文档中,但教程也鼓励你不要去想它,然后当你需要弄明白时,你不想挖。 :) –

回答

158
import (
    "text/template" 
    htemplate "html/template" // this is now imported as htemplate 
) 

了解更多关于它in the spec

+1

你说得对。它的工作原理。谢谢 – hardPass

+2

@hardPass:请点击此答案旁边的“打勾”图标,表示它是正确和被接受的答案。 – Ashe

+1

这是一个完美的答案:向您显示您需要知道的内容,同时也提供了更多信息的链接。好样的! –

相关问题