Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 578
Casper Avatar asked May 19 '16 16:05

Casper


People also ask

How do I create a template in Golang?

Here List should be an array, map or slice which if has length 0, then template temp_1 will be executed, else it iterates through the elements on List. The input format for a template should be of a UTF-8-encoded format. Any other text outside is printed as it is to the standard output. The predefined variable os.


2 Answers

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")
like image 78
Muhammad Soliman Avatar answered Nov 14 '22 20:11

Muhammad Soliman


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.

like image 30
Bayta Darell Avatar answered Nov 14 '22 21:11

Bayta Darell