Golang HTTP服务中panic报错:无法找到templates\chat.html路径的解决方案
Golang HTTP服务中panic报错:无法找到templates\chat.html路径的解决方案 我是Go编程语言的新手,尝试了以下代码:
package main
import (
"log"
"net/http"
"path/filepath"
"sync"
"text/template"
)
// templ represents a single template
type templateHandler struct {
once sync.Once
filename string
templ *template.Template
}
// ServeHTTP handles the HTTP request.
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.once.Do(func() {
t.templ = template.Must(template.ParseFiles(filepath.Join("templates",
t.filename)))
})
t.templ.Execute(w, nil)
}
func main() {
// root
http.Handle("/", &templateHandler{filename: "chat.html"})
// start the web server
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
当我运行它时,显示以下错误:
2019/07/09 08:26:06 http: panic serving [::1]:54218: open templates\chat.html: The system cannot find the path specified.
goroutine 19 [running]:
net/http.(*conn).serve.func1(0xc000050a00)
C:/Go/src/net/http/server.go:1769 +0x140
panic(0x73ec80, 0xc0000e80c0)
C:/Go/src/runtime/panic.go:522 +0x1c3
text/template.Must(...)
C:/Go/src/text/template/helper.go:23
main.(*templateHandler).ServeHTTP.func1()
C:/gocode/src/github.com/myTraining/chats/main.go:21 +0x124
sync.(*Once).Do(0xc00006cba0, 0xc0000d9c70)
C:/Go/src/sync/once.go:44 +0xba
main.(*templateHandler).ServeHTTP(0xc00006cba0, 0x80e8c0, 0xc0000fc000, 0xc0000e0000)
C:/gocode/src/github.com/myTraining/chats/main.go:20 +0x5b
net/http.(*ServeMux).ServeHTTP(0xa57740, 0x80e8c0, 0xc0000fc000, 0xc0000e0000)
C:/Go/src/net/http/server.go:2375 +0x1dd
net/http.serverHandler.ServeHTTP(0xc00004ab60, 0x80e8c0, 0xc0000fc000, 0xc0000e0000)
C:/Go/src/net/http/server.go:2774 +0xaf
net/http.(*conn).serve(0xc000050a00, 0x80ee40, 0xc000052440)
C:/Go/src/net/http/server.go:1878 +0x858
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2884 +0x2fb
2019/07/09 08:26:06 http: panic serving [::1]:54219: runtime error: invalid memory address or nil pointer dereference
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc000138000)
C:/Go/src/net/http/server.go:1769 +0x140
panic(0x73bdc0, 0xa4b820)
C:/Go/src/runtime/panic.go:522 +0x1c3
text/template.errRecover(0xc0000d9c58)
C:/Go/src/text/template/exec.go:160 +0x1d4
panic(0x73bdc0, 0xa4b820)
C:/Go/src/runtime/panic.go:522 +0x1c3
text/template.(*Template).execute(0x0, 0x808dc0, 0xc0000fc0e0, 0x0, 0x0, 0x0, 0x0)
C:/Go/src/text/template/exec.go:214 +0x1a9
text/template.(*Template).Execute(...)
C:/Go/src/text/template/exec.go:200
main.(*templateHandler).ServeHTTP(0xc00006cba0, 0x80e8c0, 0xc0000fc0e0, 0xc00013c000)
C:/gocode/src/github.com/myTraining/chats/main.go:24 +0xb8
net/http.(*ServeMux).ServeHTTP(0xa57740, 0x80e8c0, 0xc0000fc0e0, 0xc00013c000)
C:/Go/src/net/http/server.go:2375 +0x1dd
net/http.serverHandler.ServeHTTP(0xc00004ab60, 0x80e8c0, 0xc0000fc0e0, 0xc00013c000)
C:/Go/src/net/http/server.go:2774 +0xaf
net/http.(*conn).serve(0xc000138000, 0x80ee40, 0xc0000f2180)
C:/Go/src/net/http/server.go:1878 +0x858
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2884 +0x2fb
2019/07/09 08:26:06 http: panic serving [::1]:54220: runtime error: invalid memory address or nil pointer dereference
goroutine 37 [running]:
net/http.(*conn).serve.func1(0xc0001380a0)
C:/Go/src/net/http/server.go:1769 +0x140
panic(0x73bdc0, 0xa4b820)
C:/Go/src/runtime/panic.go:522 +0x1c3
text/template.errRecover(0xc0000d9c58)
C:/Go/src/text/template/exec.go:160 +0x1d4
panic(0x73bdc0, 0xa4b820)
C:/Go/src/runtime/panic.go:522 +0x1c3
text/template.(*Template).execute(0x0, 0x808dc0, 0xc0000fc1c0, 0x0, 0x0, 0x0, 0x0)
C:/Go/src/text/template/exec.go:214 +0x1a9
text/template.(*Template).Execute(...)
C:/Go/src/text/template/exec.go:200
main.(*templateHandler).ServeHTTP(0xc00006cba0, 0x80e8c0, 0xc0000fc1c0, 0xc00013c100)
C:/gocode/src/github.com/myTraining/chats/main.go:24 +0xb8
net/http.(*ServeMux).ServeHTTP(0xa57740, 0x80e8c0, 0xc0000fc1c0, 0xc00013c100)
C:/Go/src/net/http/server.go:2375 +0x1dd
net/http.serverHandler.ServeHTTP(0xc00004ab60, 0x80e8c0, 0xc0000fc1c0, 0xc00013c100)
C:/Go/src/net/http/server.go:2774 +0xaf
net/http.(*conn).serve(0xc0001380a0, 0x80ee40, 0xc0000f2280)
C:/Go/src/net/http/server.go:1878 +0x858
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2884 +0x2fb
2019/07/09 08:26:06 http: panic serving [::1]:54221: runtime error: invalid memory address or nil pointer dereference
goroutine 39 [running]:
net/http.(*conn).serve.func1(0xc000138140)
C:/Go/src/net/http/server.go:1769 +0x140
panic(0x73bdc0, 0xa4b820)
C:/Go/src/runtime/panic.go:522 +0x1c3
text/template.errRecover(0xc0000d9c58)
C:/Go/src/text/template/exec.go:160 +0x1d4
panic(0x73bdc0, 0xa4b820)
C:/Go/src/runtime/panic.go:522 +0x1c3
text/template.(*Template).execute(0x0, 0x808dc0, 0xc0000fc2a0, 0x0, 0x0, 0x0, 0x0)
C:/Go/src/text/template/exec.go:214 +0x1a9
text/template.(*Template).Execute(...)
C:/Go/src/text/template/exec.go:200
main.(*templateHandler).ServeHTTP(0xc00006cba0, 0x80e8c0, 0xc0000fc2a0, 0xc00013c200)
C:/gocode/src/github.com/myTraining/chats/main.go:24 +0xb8
net/http.(*ServeMux).ServeHTTP(0xa57740, 0x80e8c0, 0xc0000fc2a0, 0xc00013c200)
C:/Go/src/net/http/server.go:2375 +0x1dd
net/http.serverHandler.ServeHTTP(0xc00004ab60, 0x80e8c0, 0xc0000fc2a0, 0xc00013c200)
C:/Go/src/net/http/server.go:2774 +0xaf
net/http.(*conn).serve(0xc000138140, 0x80ee40, 0xc0000f2380)
C:/Go/src/net/http/server.go:1878 +0x858
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2884 +0x2fb
2019/07/09 08:26:06 http: panic serving [::1]:54222: runtime error: invalid memory address or nil pointer dereference
goroutine 41 [running]:
net/http.(*conn).serve.func1(0xc0001381e0)
C:/Go/src/net/http/server.go:1769 +0x140
panic(0x73bdc0, 0xa4b820)
C:/Go/src/runtime/panic.go:522 +0x1c3
更多关于Golang HTTP服务中panic报错:无法找到templates\chat.html路径的解决方案的实战教程也可以访问 https://www.itying.com/category-94-b0.html
是的,模板文件夹中存在 chat.html 文件,但仍然显示相同的错误
更多关于Golang HTTP服务中panic报错:无法找到templates\chat.html路径的解决方案的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
请检查您的文件是否确实存在于"templates"目录中。请注意,templates目录应位于运行的可执行文件附近,而不是其他位置。
附注:请格式化代码并将其放入特殊标签中。
上面的代码对我来说是有效的。你确定应用程序是从正确的目录启动的吗?
可以通过以下代码检查:
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
fmt.Println(dir)
尝试上面的代码后显示的是我的GO文件夹,请问你的main.go和chat.html是在同一个文件夹还是不同的文件夹?另外当我尝试在浏览器中查看时显示"连接被重置
页面加载时与服务器的连接被重置。
网站可能暂时不可用或过于繁忙。请稍后再试。
如果无法加载任何页面,请检查计算机的网络连接。
如果您的计算机或网络受到防火墙或代理保护,请确保Firefox被允许访问网络。"
错误是由于Go程序无法找到模板文件路径导致的。问题出现在templateHandler的ServeHTTP方法中,当第一次尝试解析模板时,如果文件不存在,template.Must会触发panic。后续请求由于sync.Once只执行一次,t.templ保持为nil,导致执行模板时出现空指针解引用。
以下是修复后的代码:
package main
import (
"log"
"net/http"
"path/filepath"
"sync"
"text/template"
)
type templateHandler struct {
once sync.Once
filename string
templ *template.Template
err error
}
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.once.Do(func() {
t.templ, t.err = template.ParseFiles(filepath.Join("templates", t.filename))
})
if t.err != nil {
http.Error(w, "Template file not found", http.StatusInternalServerError)
return
}
if err := t.templ.Execute(w, nil); err != nil {
http.Error(w, "Template execution failed", http.StatusInternalServerError)
}
}
func main() {
http.Handle("/", &templateHandler{filename: "chat.html"})
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
主要修改:
- 移除了
template.Must,改用直接错误处理 - 添加了错误字段来存储解析错误
- 在模板执行前检查错误,避免空指针解引用
- 添加了模板执行错误的处理
确保你的项目目录结构包含templates文件夹,且其中有chat.html文件:
your-project/
├── main.go
└── templates/
└── chat.html
如果模板文件路径仍然有问题,可以使用绝对路径:
t.once.Do(func() {
t.templ, t.err = template.ParseFiles(filepath.Join("./templates", t.filename))
})
或者使用工作目录的相对路径:
t.once.Do(func() {
t.templ, t.err = template.ParseFiles("templates/" + t.filename)
})


