How to specify the file location for `template.ParseFiles` in Go Language?

12,907

Solution 1

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.

Solution 2

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")
Share:
12,907
Casper
Author by

Casper

宗教能給你無限的啟發和想像,但科學才能給你完整的答案。 RELIGION can give you unlimited inspiration and immagenation, while SCIENCE can give you a complete answer.

Updated on June 11, 2022

Comments

  • Casper
    Casper almost 2 years

    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
    Casper almost 8 years
    I wonder should I start the server program first and it may provide something like public folder.
  • RedBlue
    RedBlue over 4 years
    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.