Html/template: pattern matches no files

I’m working on really simple Go project, and I’m using http/template.
First thing, files structure is

 - cmd/ 
   - main.go
 - templates/
   - pages.tmpl
   - page.tmpl
   - post.tmpl
 - db.go
 - db_test.go
 - handler.go
 - template.go

Part of template.go is

 package cms

 import (

var Tmpl = template.Must(template.ParseGlob("templates/*"))

When I run go run main.go from cmd folder I’m getting html/template: pattern matches no files: "templates/*" error. And when I run go test from home page, all tests are passing. To make go run main.go working I have to change pattern to ("../templates/*") but in that case go test will fail.

I guess that is something with building process that I still don’t understand. Can someone explain it to me?

When you are inside home/cmd, that is your current directory. Looking for templates/* at that point means looking for home/cmd/templates/* which is incorrect.

Generally, don’t use go run. Use go install to create a binary. Run it, from the place you want to be your current directory.

Perhaps even more generally, realize that the templates you mention are not compiled in, they are loaded at runtime. (Maybe you were totally aware of this, just mentioning for completeness.)


@calmh is right - your issue stems from where you run your code. If you want more info on this error check out this page -

I’ll have to update the site to also include the error message you received.

1 Like

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