小编典典

如何导入和使用不同的同名包

all

例如,我想在一个源文件中同时使用 text/template 和 html/template。但是下面的代码会抛出错误。

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}}`)

}

阅读 101

收藏
2022-08-29

共1个答案

小编典典

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

在规范中阅读有关它的更多信息。

2022-08-29