Я не могу получить статические шаблоны. Вот мой код СтруктураGo: служить статическим шаблонам
Перейти Каталог
src
/github.com
/sam
/hello
auth.go
main.go
/templates
signup.html
auth.go
package main
//...
func homeHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "signup", nil)
}
func renderTemplate(w http.ResponseWriter, tmpl string, user *data.User) {
t := template.Must(template.New("tele").ParseFiles("templates/" + tmpl + ".html"))
err := t.ExecuteTemplate(w, tmpl, user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
main.go
package main
func main() {
//...
http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir("templates"))))
//...
}
signup.html
{{ define "signup" }}
//html code
{{ end }}
Ран go install github.com/sam/auth
и открыл localhost:3000
, но я все еще получаю ошибку паники:
open templates/signup.html: no such file or directory
ПОЧЕМУ ???
Потому что вы не запускаете свою программу в правильном каталоге, может быть? – rightfold
uhh nope, исправил ошибку редактирования. – user3918985
Итак, каков ваш текущий рабочий каталог? Запускаете ли вы приложение из корневого каталога вашего проекта? См. [Этот вопрос SO] (http://stackoverflow.com/questions/19035187/go-specifying-template-filenames-for-template-parsefiles) – Makpoc