Http: panic serving [::1]:54218: open templates\chat.html: The system cannot find the path specified. goroutine 19 [running]:


(Ubong Ndoh) #1

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

( Kvaz1r) #2

Check if your file really exist in dir “templates”. Keep in mind that templates directory should be near .exe running file and not somewhere else.

P.S. Please, format code and put it into special tags.


(Ubong Ndoh) #3

Yes the file chat.html exist in the templates folder but it still showing the same error


( Kvaz1r) #4

Code above works for me. Are you sure that application starts from right dirs?
Check it with:

  dir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
  fmt.Println(dir)

(Ubong Ndoh) #5

try the code above it is showing my GO folders, please is your main.go and chat.html in the same folder or different and also when i try viewing it on my browser it is showing "The connection was reset

The connection to the server was reset while the page was loading.

The site could be temporarily unavailable or too busy. Try again in a few moments.
If you are unable to load any pages, check your computer’s network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web."

( Kvaz1r) #6

Of course in different. chat.html according to the code should be in child templates directory.
You can try use absolute path to your folder and only after that change to relative.


(system) closed #7

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.