Using a GitHub template repo for Golang projects

Using a GitHub template repo for Golang projects

August 13, 2024

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:

  1. Golang
  2. Templ - for html+go templating
  3. Air - for hot reloads
  4. Echo - the golang http server framework
  5. Supabase - for DB and auth
  6. TailwindCSS - works great with Templ and the rest of the stack
  7. 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:

  1. You clone a repo from a template
  2. You manually go through all the import paths to convert them from the template name to the name of your new project
  3. 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.

Last updated on