Templates in Subdirectories

Hi, sorry, as I can’t find anywhere else where this question has been asked I know this has to be a real newb question but … please can anyone share the magic of referencing text templates in a subdirectory? It’s driving me crazy!!!


  1. Take a template tutorial and copy/paste so that the golang source and template are in the same directory
  2. Build the go source as a Windows .exe and execute.
  3. Success, it works perfectly and the crowd goes wild - I can successfully copy someone elses work!
  4. Create a templates subdirectory and copy the template file into it
  5. Manipulate the source to point to the template in the subdirectory (eg:
    dir, _ := filepath.Abs("templates")
    templateFile := filepath.Join(dir, "email.tmpl")
  6. Compile and execute …
  7. Scream as you’re told that the template is an “incomplete or empty template”

I’ve tried this every way I can think of but am consistently failing unless I leave the template in the same directory. Please can you put me out of my misery?

I think you need to use ParseFiles instead of Parse

Thanks Yamil, that was just a copy/paste error on my part. I’m pretty sure that the problem is with the ''s in the path but they’re all properly escaped when I output to screen … just can’t figure out the magic sequence so might just dump the templates in the same directory and stop worrying about it!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.