After I watched this video, I try it myself. However, I get the panic error panic: open templates/index.html: The system cannot find the path specified.
The Complete erroe message is like the following.
Hello, Go Web Development 1.3
panic: open templates/index.html: The system cannot find the path specified.
goroutine 1 [running]:
panic(0x789260, 0xc082054e40)
F:/Go/src/runtime/panic.go:481 +0x3f4
html/template.Must(0x0, 0xe34538, 0xc082054e40, 0x0)
F:/Go/src/html/template/template.go:340 +0x52
main.main()
E:/Users/User/Desktop/codespace/go_workspace/src/go-for-web-dev/src/1.3_UsingTemplate.go:11 +0x20d
I have tried different string like "templates/index.html"
, "index.html"
, "./template/index.html"
... Also, I try to copy the entire template folder into pkg
, bin
...But I get the same error message.
The following is the go program (1.3_UsingTemplate.go).
package src
import (
"fmt"
"net/http"
"html/template"
)
func main() {
fmt.Println("Hello, Go Web Development 1.3")
templates := template.Must(template.ParseFiles("templates/index.html")) //This line should have some problem
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := templates.ExecuteTemplate(w, "index.html", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
fmt.Println(http.ListenAndServe(":8080",nil))
}
File Structure
To solve this problem, I need to first change the current working directory to the folder containing the *.go file. Then, execute go run {filename.go}
. In GoClipse, is there any setting can be set to the Run Configurations
for automatically changing the current working directory to the folder containing the *.go file?
You specified a path relative to the current working directory. This directory may not have any relationship with the directory containing the source code.
Change directory to E:/Users/User/Desktop/codespace/go_workspace/src/go-for-web-dev/src
to run your program. The path to the template is relative to this directory.