相关疑难解决方法(0)

如何在Go语言中导入和使用同名的不同包?

例如,我想在一个源文件中同时使用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}}`)

}
Run Code Online (Sandbox Code Playgroud)

packages go

123
推荐指数
2
解决办法
4万
查看次数

标签 统计

go ×1

packages ×1