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

huangapple go评论87阅读模式
英文:

How to import and use different packages of the same name

问题

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

import (
    "fmt"
    "net/http"
    "text/template" // 模板重复声明为导入的包名
    "html/template" // 模板重复声明为导入的包名
)

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}}`)
}
英文:

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

}

答案1

得分: 331

import (
"text/template"
htemplate "html/template" // 这个现在被导入为htemplate
)

在规范中阅读更多关于它的内容 in the spec.

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

Read more about it in the spec.

答案2

得分: 40

Answer by Mostafa is correct, however it demands some explanation. Let me try to answer it.

Your example code doesn't work because you're trying to import two packages with the same name, which is: "template".

import "html/template"  // imports the package as `template`
import "text/template"  // imports the package as `template` (again)

Importing is a declaration statement:

  • You can't declare the same name (terminology: identifier) in the same scope.

  • In Go, import is a declaration and its scope is the file that's trying to import those packages.

  • It doesn't work because of the same reason that you can't declare variables with the same name in the same block.

The following code works:

package main

import (
    t "text/template"
    h "html/template"
)

func main() {
    t.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
    h.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
}

The code above gives two different names to the imported packages with the same name. So, there are now two different identifiers that you can use: t for the text/template package, and h for the html/template package.

You can check it on the playground.

英文:

Answer by Mostafa is correct, however it demands some explanation. Let me try to answer it.

Your example code doesn't work because you're trying to import two packages with the same name, which is: " template ".

import "html/template"  // imports the package as `template`
import "text/template"  // imports the package as `template` (again)

Importing is a declaration statement:

  • You can't declare the same name (terminology: identifier) in the same scope.

  • In Go, import is a declaration and its scope is the file that's trying to import those packages.

  • It doesn't work because of the same reason that you can't declare variables with the same name in the same block.

The following code works:

package main

import (
    t "text/template"
    h "html/template"
)

func main() {
    t.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
    h.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
}

The code above gives two different names to the imported packages with the same name. So, there are now two different identifiers that you can use: t for the text/template package, and h for the html/template package.

You can check it on the playground.

huangapple
  • 本文由 发表于 2012年5月2日 14:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/10408646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定