I am new to Go programming language, i try this code
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)
}
}
when i run it, it’s showing this error
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