HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

How to import and use different packages of the same name

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
hownameandsameusepackagesthedifferentimport

Problem

For example, I want to use both the text/template and html/template packages 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}}`)

}

Solution

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


Read more about it in the spec.

Code Snippets

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

Context

Stack Overflow Q#10408646, score: 353

Revisions (0)

No revisions yet.