如何导入和使用相同名称的不同软件包
问题描述:
例如,我想在一个源文件中同时使用text/template和html/template. 但是下面的代码会引发错误.
For example, I want to use both text/template and html/template in one source file. But the code below throw errors.
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}}`)
}
答
import (
"text/template"
htemplate "html/template" // this is now imported as htemplate
)
详细了解它规范中的.