8

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

enter image description here


Update

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?

Casper
  • 4,435
  • 10
  • 41
  • 72

2 Answers2

6

os.Getwd() can be used to return the root working directory of your project and then concatenate with the inner path of your template file:

// Working Directory
wd, err := os.Getwd()
if err != nil {
   log.Fatal(err)
}
// Template
tpl, err := template.ParseFiles(wd + "/templates/index.html")
Seb
  • 888
  • 12
  • 20
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • 6
    `os.Getwd()` returns the working directory, not the root directory of the project. Because the operating system uses the working directory to resolve relative paths to an absolute path, the code in this answer does the same thing as the code in the question. –  Dec 05 '19 at 08:24
4

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.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • I wonder should I start the server program first and it may provide something like `public` folder. – Casper May 19 '16 at 16:45