2012-05-02 3 views
82

예를 들어 하나의 소스 파일에서 text/template과 html/template을 모두 사용하고 싶습니다. 그러나 아래 코드는 오류를 발생시킵니다.Go 언어에서 같은 이름의 다른 패키지를 가져와 사용하는 방법은 무엇입니까?

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

이 질문에 감사드립니다. 물론 그것은 문서에 있지만 자습서는 또한 당신이 그것에 대해 생각하지 않도록 권장합니다. 그리고 나서 당신이 그것을 알아 내야 할 때 당신은 파기를 원하지 않습니다. :) –

답변

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

in the spec에 대해 자세히 알아보십시오.

+1

당신 말이 맞아요. 작동합니다. 감사합니다. – hardPass

+2

@ hardPass :이 답변 옆에있는 "진드기"아이콘을 클릭하여 올바른 대답으로 알려주십시오. – Ashe

+1

이것은 완벽한 대답입니다 : 알아 두어야 할 정보를 보여줄뿐만 아니라 더 많은 정보를 얻을 수있는 링크를 제공합니다. 브라보! –

관련 문제