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

标签 go package

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

}

最佳答案

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

阅读更多信息 in the spec .

关于go - 如何导入和使用不同的同名包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10408646/

相关文章:

pointers - 删除指针值不会 panic

go - 尝试检查路径时出现错误

r - 在 R 中,您可以允许用户仅从某些函数导入命名空间吗?

Python嵌套包问题

Python - 如何定义另一个库源

r - "The following object is masked from ' 包 :xxx'"mean? 是什么意思

linux - 找不到包 'pygobject-3.0'

go - 接口(interface)的结构嵌入, panic : runtime error

go - 使用GoLand基于grpc proto方法生成server.go

go - 无法通过 Upstart 启动 Golang Prog