1

I am trying to load an html file from a directory and I am getting the error "open templates: no such file or directory"

My directory structure is below

/Users/{username}/go/src/app main.go

/Users/{username}/go/src/app/templates mytemplate.html

The error is coming from the line below

template.Must(template.ParseFiles(filepath.Join("templates", "mytemplate.html")))

I am new to go and just trying to get a feel for the syntax.

EDIT 1

I am building the project using the "go build" command and executing it out of the "app" directory shown above.

$GOROOT = /usr/local/go $GOPATH = /Users/{username}/go

I also updated the directory structure to integrate the $GOPATH

Jake
  • 21
  • 4
  • Where are you running your program from? See this for information about relative path in Go : https://stackoverflow.com/questions/17071286/how-can-i-open-files-using-relative-paths-in-go – HectorJ Sep 14 '15 at 01:29
  • The templates folder is actually under the app directory. I updated the question to clear it up. – Jake Sep 14 '15 at 01:37
  • 1
    Are you using `go run` or `go install` for your executable? Remember that `go install` puts the binary in `$GOPATH/bin` folder. – SnoProblem Sep 14 '15 at 01:45
  • 1
    I was using go build I was able to put in the absolute path to the file which resolved the issue. Then using filepath.Join() to vary the file name and folder name. My issue seems to be the same as the question seen in the link from @HectorJ. – Jake Sep 14 '15 at 01:56

2 Answers2

1

Check the working directory that your program has at runtime with

dir, _ := os.Getwd()
fmt.Println(dir)

Then you can use that to get the right path for the templates

template.Must(template.ParseFiles(filepath.Join(dir, "templates", "mytemplate.html")))

For production use you could get the val of dir fro a config file or the environment,

ref : https://golang.org/pkg/os/#Getwd

EDIT: When you run the program make sure you are in the correct directory using cd in your terminal

LenW
  • 3,054
  • 1
  • 24
  • 25
  • I tried this and it did not work. The working directory returned "/Users/{username}/templates/mytemplate.html" and the actual location is "/Users/{username}/go/src/{projectname}/templates/mytemplate.html" – Jake Sep 17 '15 at 02:01
  • So as per the comments above your working directory is no correct, how are you running you program ? – LenW Sep 17 '15 at 04:47
  • Using "go build" then executing it out of the /Users/{username}/go/src/app directory. – Jake Sep 17 '15 at 13:27
  • copy your templates under that app directory – LenW Sep 17 '15 at 13:33
-1

Try this,

template.Must(template.New("mytemplate.html").ParseFiles("templates/mytemplate.html"))
Priyanka
  • 232
  • 3
  • 12
  • 4
    Please edit with more information. Code-only and "try this" answers are [discouraged](http://meta.stackexchange.com/questions/196187/is-try-this-bad-practice), because they contain no searchable content, and don't explain why someone should "try this". – Rick Smith Sep 14 '15 at 17:06
  • I tried this, but I received the same error as the original posting. – Jake Sep 17 '15 at 02:07