【发布时间】:2017-08-27 19:06:28
【问题描述】:
执行模板时我无法在 go lang 中找出这个错误
panic: open templates/*.html: The system cannot find the path specified.
另一个问题是我的公用文件夹无法从 css 提供,我不知道为什么。
代码:
package main
import (
"net/http"
"github.com/gorilla/mux"
"html/template"
"log"
)
var tpl *template.Template
func init() {
tpl = template.Must(template.ParseGlob("templates/*.html"))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/",home)
http.Handle("/public/", http.StripPrefix("/public/",
http.FileServer(http.Dir("/pub"))))
http.ListenAndServe(":8080", r)
}
func home(writer http.ResponseWriter, request *http.Request) {
err := tpl.ExecuteTemplate(writer, "index.html", nil)
if err != nil {
log.Println(err)
http.Error(writer, "Internal server error", http.StatusInternalServerError)
}
}
我的文件夹是这样的:
bin
包
酒馆
css
主页.css
js
源代码
github.com
大猩猩/多路复用器
模板
index.html
我的 GOROOT 指向文件夹 project,其中包含 bin pkg src
当我在src 之外创建templates 文件夹时,它工作正常,但我认为这是不对的,所有内容都必须在src 中,对吗?
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link href="../public/css/home.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1>welcome! hi</h1>
</body>
</html>
这里我不能提供 css
更新::
模板文件夹的第一个问题解决了。
但我仍然无法解决第二个问题
【问题讨论】:
-
templates 文件夹只需要靠近您在构建二进制文件时创建的二进制文件。 src 文件夹仅对源文件是必需的。因为模板是在运行时加载的
-
所以你的意思是当我去生产时我将使用 bin 文件夹而不是 src 文件夹?所以我很抱歉地问,那么 src 在生产中是用来做什么的?我的意思是如果有人使用我的代码,他将如何使用我的代码?来自包裹?
-
所以你的意思是我从
src取出文件夹并将其放在project文件夹下,然后将parseGlob从templates/*.html更改为src/templates/*.html? -
templates/*.html与您启动二进制文件的位置相关,它与 golang 设置的其余部分无关。如果您要构建二进制文件并将其保留在 bin 中,那么您希望将模板文件夹复制到那里。如果你要将它放在 pkg 中,那么你需要在那里有模板。 -
我也将它用于公共文件夹吗?我把 public 和 template 文件夹都放在了 src 之外,我把它们放在 bin 旁边的项目文件夹下?
标签: go go-templates fileserver go-html-template gopath