162

たとえば、1 つのソース ファイルで 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}}`)

}
4

2 に答える 2

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

詳細については、仕様をご覧ください。

于 2012-05-02T06:24:50.067 に答える