I'm starting to experiment with Go and I'm facing an issue that (I think) doesn't exist in languages that use a virtual machine.
I have a src/test/main.go
file that references my templates inside src/test/views/
folder. When I use go run main.go
it runs but when do go install
and then inside my bin
folder run the executable (./test
) I get an error:
views/edit.html: no such file or directory
So, how does Go compiles my project (file/folder structure related) and how to use paths in a way that allows me to use either go run
and go install/executable
?
If you specify a relative path in your code, as in views/edit.html
it will also be looked up relative to the binary location. You need to either make the path absolute, or use some logic to determine where your templates will be located.
Another option would be to use https://github.com/jteeuwen/go-bindata or https://github.com/elazarl/go-bindata-assetfs that will save you the hassle.