Introduction

A problem I frequently encounter in my actual work is: when creating a new project, I often need to manually do a lot of things, such as project configuration, observability integration, and some infrastructure access…

When creating new projects, different project leads may have different preferences that are difficult to standardize. Even the same person can hardly guarantee consistency. After experiencing this several times, we decided to use Project templates to manage this common code and avoid repetitive work.

However, when initializing projects, we still use Git Clone to create new projects. Compared to laravel/installer and front-end project scaffolding, this can solve the problem, but it’s obviously not the optimal solution!

I found that Go officially published a blog post Experimenting with project templates, which introduces an experimental project golang.org/x/tools/cmd/gonew. With it, we can quickly create a project using templates.

The main principle is: download a Module template, change its Module path, and place it in a new directory locally.

Installation

go install golang.org/x/tools/cmd/gonew@latest

Usage

The gonew command syntax is as follows:

gonew
usage: gonew srcmod[@version] [dstmod [dir]]
See https://pkg.go.dev/golang.org/x/tools/cmd/gonew.
  • srcmod: The address of the source Module path, such as github.com/ServiceWeaver/template
  • @version: Download a specific version of the template, such as github.com/ServiceWeaver/[email protected]. If not specified, the latest version is used
  • dstmod: The destination Module path. If not specified, the source Module path is used
  • dir: The name of the local directory to save to

Here are specific usage examples:

# Download the latest version
gonew github.com/ServiceWeaver/template

# Download a specific version
gonew github.com/ServiceWeaver/[email protected]

# Download the specified template and replace the Go module path with gitlab.test/services/backend
gonew github.com/ServiceWeaver/template gitlab.test/services/backend

# Download the specified template, replace the Go module path with gitlab.test/services/backend, and specify the local directory name
gonew github.com/ServiceWeaver/template gitlab.test/services/backend backend

Gitlab Private Projects

We use Self-hosted Gitlab as our code management system, and project templates are similar to private Modules.

gonew gitlab.test/services/sendbox
gonew: go mod download -json gitlab.test/services/sendbox@latest: exit status 1
{
	"Path": "gitlab.test/services/sendbox",
	"Version": "v1.3.0",
	"Query": "latest",
	"Error": "gitlab.test/services/[email protected]: verifying go.mod: gitlab.test/services/[email protected]/go.mod: Get \"https://proxy.golang.org/sumdb/sum.golang.org/supported\": dial tcp 142.251.42.241:443: i/o timeout",
	"Info": "/Users/George/Develop/Go/pkg/mod/cache/download/gitlab.test/services/sendbox/@v/v1.3.0.info",
	"Origin": {
		"VCS": "git",
		"URL": "https://gitlab.test/services/sendbox.git",
		"TagSum": "t1:8u5g92aMnUe25PzyvfLHGI+FiAP55e2BtcL+03OfxwA=",
		"Ref": "refs/tags/v1.3.0",
		"Hash": "9f6d23e699a844087d2d429a45379245b91e86d4"
	}
}

If you encounter the above problem, you need to add the Gitlab domain to the GONOSUMDB environment variable to avoid verification!

go env -w GONOSUMDB="gitlab.test"
gonew gitlab.test/services/sendbox
gonew: initialized gitlab.test/services/sendbox in ./sendbox

Conclusion

Before gonew appeared, many frameworks developed their own scaffolding, but most were targeted. After gonew came into being, this work was simplified, allowing us to focus only on business.

For some CI/CD configurations, such as Dockerfile, docker-compose.yaml, etc., they can all be managed uniformly in the Project template. Through this convention-over-configuration collaboration mode, the communication cost between team members is greatly reduced!

I hope this is helpful, Happy hacking…