An enhanced version of gonew that supports customizable template replacements

Introduction⌗
I previously wrote an article about gonew
: Quickly Create Go Projects from Templates. While it does reduce the boilerplate overhead of setting up a new project, it’s still far from perfect.
In the Go repository, Russ Cox initiated a discussion on gonew: templates for new modules. The community had many ideas, but the Go team doesn’t seem very motivated to maintain or evolve a CLI tool like gonew
.
With my own needs and some of the community suggestions in mind, I decided to build a more powerful CLI scaffold tool based on golang.org/x/tools/cmd/gonew@latest
.
Features⌗
In addition to the basic functionality of the official gonew
, this enhanced version supports interactive input for customizable project templates, which allows code snippet placeholders to be dynamically replaced.
Once a project is initialized, the tool reads the template.yaml
file in the template module, launches an interactive CLI prompt to collect user input, and replaces all placeholder variables in the code accordingly.
Installation⌗
go install github.com/betterde/gonew@latest
Usage⌗
gonew init <SOURCE_MODULE> [DEST_MODULE]
Here’s an example of initializing a project using my own template:
gonew init github.com/betterde/template/fiber github.com/betterde/testing
Please enter project name: testing
Please enter project description: Testing gonew init project with template
2025/05/07 09:34:43 initialized github.com/betterde/testing in ./testing
As you can see, once the module is downloaded, the CLI prompts the user for project-specific details.
Creating a Template Module⌗
In my github.com/betterde/template/fiber
template module, for example, I define internal/build/info.go
as follows:
package build
var (
Name = "{{ .name }}"
Desc = "{{ .desc }}"
Build = "current"
Commit = "none"
Version = "develop"
)
This file defines essential configuration and placeholder variables for the project. Additionally, you’ll need to create a template.yaml
file in the root directory of the module, which specifies the variable names and their corresponding prompts. In my template, it looks like this:
name: "Please enter project name"
desc: "Please enter project description"
That’s why during initialization, you’ll see the interactive prompts on lines 2–3 of the example above.
Conclusion⌗
With these features, this tool covers about 90% of what most teams need when initializing Go projects. For more advanced customizations, you can further extend the template.yaml
format and enhance the CLI behavior.
I hope this is helpful. Happy hacking…