Using a GitHub template repo for Golang projects
Github templates
Github templates are a great way to speed projects. I have a few pet project templates that I reuse often and it saves me a ton time since all the boilerplate is ready to go.
My recent project template
Recently, I developed a project template utilizing the following technology stack:
- Golang
- Templ - for html+go templating
- Air - for hot reloads
- Echo - the golang http server framework
- Supabase - for DB and auth
- TailwindCSS - works great with Templ and the rest of the stack
- Google Cloud - for deploying, building and running the webserver
This setup has been incredibly enjoyable, enabling me to experiment with new ideas without repeatedly dealing with boilerplate. I just create a new github repo using the template and I am good to go.
The downside
A major drawback I encountered was the need to manually rewrite the import paths in Go. I tried to look for a few different tools but they didn’t really do what I wanted.
Then I returned to reading the Go docs for something else and happened to glance at gofmt
docs once again. And guess what? The gofmt
rewrite rules can be used for this and I didn’t need to futz around with other
third party tools.
The solution
Here is the scenario:
- You clone a repo from a template
- You manually go through all the import paths to convert them from the template name to the name of your new project
- The process is manual and slow, sparking frustration for any efficiency-minded engineer…
The fix:
find cmd -maxdepth 10 -type d \
-exec \
gofmt -w -r '"github.com/urjitbhatia/templateproject/'"{}"'" -> "github.com/urjitbhatia/newproject/'"{}"'"' ./**/*.go ';'
And voilà! The paths are automatically rewritten.
Satisfying shell one-liners!
There’s something incredibly satisfying about crafting a concise, one-liner bash command to automate tasks effortlessly!
Hope others find this useful too.