-2

I'm trying to serve an html file with echo but whenever I run the program I get that panic error.

// where:
type TemplateRenderer struct {
    templates *template.Template
}

func genTemplate(path string) *TemplateRenderer {
    tr := &TemplateRenderer{
        templates: template.Must(template.ParseFiles(path)),
    }

    return tr
}
e := echo.New()

renderer := genTemplate("./templates/*.html")
e.Renderer = renderer
panic: template: pattern matches no files: `./templates/*.html`

I've looked around but can't seem to find a solution to the problem. Any ideas? Let me know if any more information is needed.

5
  • The directory with the templates is located in the same directory the binary is outputted to. The path indicates where in the directory is the template dir Commented May 21, 2025 at 12:15
  • 1
    A relative path is relative to your current working directory, not the location of the binary you are calling. Commented May 21, 2025 at 12:19
  • Do you have a reliable solution to fix this issue? Commented May 21, 2025 at 12:26
  • You can do any of the same things for any application that needs to locate files. Check well-known locations, pass in a path at runtime via configuration, flags or environment variables, check things relative to the executable path and so on. Commented May 21, 2025 at 12:29
  • 2
    You can also directly embed the files in your binary using the embed package Commented May 21, 2025 at 12:37

2 Answers 2

1

The template.ParseFile() does not support glob patterns , so it expects the specific file path .
so use template.ParseGlob(). like this :

templates: template.Must(template.ParseGlob(path))

Sign up to request clarification or add additional context in comments.

Comments

-1

Documentation says:

Must is a helper that wraps a call to a function returning (*Template, error) and panics if the error is non-nil It is intended for use in variable initializations such as var t = template.Must(template.New("name").Parse("text")). (https://pkg.go.dev/text/template#Must)

Panic is useful, if your program abnormally parses the template, but not when you searching files.

Just use your internal part template.ParseFiles(path) And check for error by yourself

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.