Text of github.com › golang › go › discussions › 61669 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

gonew: templates for new modules · golang go · Discussion #61669 · GitHub

Skip to content You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert gonew: templates for new modules #61669 rsc announced in Discussions gonew: templates for new modules #61669 rsc Jul 31, 2023 · 51 comments · 40 replies Return to top Discussion options Uh oh!

There was an error while loading. Please reload this page . Uh oh! There was an error while loading. Please reload this page . rsc Jul 31, 2023 A common request we hear from users is some kind of 'go new' functionality to start a new module with some kind of basic template. It seems like a tool along these lines should not be limited to templates provided by the Go team: it should be possible for anyone to define templates.

That is, templates should just be ordinary modules. At that point the core job of 'go new' is mainly to download a template, change its module path, and drop it in a new directory for editing. There may be more of course, but that's the core. After literally years of occasionally batting this idea around with @Sajmani , a few months ago I wrote a very trivial text-substitution-based, untested tool at rsc.io/tmp/gonew that did this and mentioned it to a few other people.

Almost immediately, I found another use: a book author emailed asking how best to make book examples available without requiring book readers to puzzle through the vagaries of cloning a git repo. The obvious answer is gonew, which lets people use only Go tools: $ gonew book.com/mybook-examples (In this form there's no changing the module path, just clone and drop in a new directory for editing.) I've also found that form useful for grabbing a copy of some module I am using, to grep through code or to hack up and add to a go.work, like a very lightweight version of github.com/rogpeppe/gohack.

A couple teams at Google were also interested to try gonew for templating. The ServiceWeaver team was interested enough that they put go install rsc.io/tmp/gonew@latest in their getting started template docs. That both confirmed interest and scared me enough (tmp is in the name for a reason!) that I wrote a small, real, tested version at golang.org/x/tools/cmd/gonew, which I've just submitted. That tool, golang.org/x/tools/cmd/gonew, is experimental in the sense that we aren't sure what will happen next, but it is non-experimental in the sense that we promise to keep the core command line that exists today working.

That way, people can confidently post scripts like ServiceWeaver now does : $ go install golang.org/x/tools/cmd/gonew@latest $ gonew github.com/ServiceWeaver/template example.com/foo I've also updated the golang.org/x/example repo to work with gonew, so here are three sample examples: $ gonew golang.org/x/example/hello $ gonew golang.org/x/example/helloserver $ gonew golang.org/x/example/outyet I am confident there is plenty that template authors will want from 'gonew' beyond this basic functionality.

I'd like to hear more about that in the comments. We will try to identify general mechanisms that will satisfy many of the things people need. Another interesting thing about having these kinds of templates is that, with a clear way to identify them (TBD), you can imagine good support for searching for templates on pkg.go.dev and for instantiating templates in the IDE experience using gopls. If you can think of other ways that the overall Go tooling ecosystem might be changed to incorporate the idea of templates, please post them too.

This is the very very beginning of what could turn out to be an important concept or tool in the Go ecosystem. Perhaps eventually it will graduate to 'go new'. It's a bit unusual for us to publish such a tiny idea, but the core functionality seems useful enough to be worth taking the community's attention to see how it should evolve. Thanks for any feedback, and please keep the discussion respectful. You must be logged in to vote All reactions 👍 199 🎉 41 😕 1 ❤️ 81 🚀 21 👀 10 Replies: 51 comments · 40 replies Comment options Uh oh!

There was an error while loading. Please reload this page . Uh oh! There was an error while loading. Please reload this page . onionj Jul 31, 2023 And something like gonew with python: https://cookiecutter.readthedocs.io/en/stable/ You must be logged in to vote All reactions 👍 7 1 reply Comment options Uh oh! There was an error while loading. Please reload this page . bluebrown Aug 1, 2023 I hacked something like this together in go, a while back https://github.com/bluebrown/blueprint All reactions Comment options Uh oh!

There was an error while loading. Please reload this page . karitham Jul 31, 2023 Would it be reasonable to make this functionality part of the go mod init process? Since they both serve bootstrapping process, go mod init example.com/x/abc --template github.com/ServiceWeaver/template could essentially have the same functionality. You must be logged in to vote All reactions 👍 29 👎 3 👀 4 2 replies Comment options Uh oh!

There was an error while loading. Please reload this page . Delta456 Jul 31, 2023 I agree then we don't need a new subcommand for this. All reactions 👍 3 Comment options Uh oh! There was an error while loading. Please reload this page . zephyrtronium Jul 31, 2023 My concern with this is that tying it to initializing the module, even if only through the command name, might imply some limits on the structures that can be templated.

E.g., it would feel awkward for go mod init -template bocchi.rocks/aws-cdk-template bocchi.rocks/aws-cdk-project to put the go.mod in a lambda/ subdirectory, or for it to create a go.work file anywhere. All reactions 👍 7 ❤️ 2 Comment options Uh oh! There was an error while loading. Please reload this page . earthboundkid Jul 31, 2023 I wrote my own template tool inspired by Python Cookiecutter that uses txtar format a few years back. https://github.com/carlmjohnson/springerle I think this makes sense as a thing to build into Go.

For example, Node has a thing where npx create foo will automatically run a create-foo package (I forget the precise details of how it works). Another tool to look at is degit from the creator of Svelte, which clones a git repo while stripping out certain info. Anyhow, it’s good to hear Alan Donovan is working on GoPL2.0. 😊 You must be logged in to vote All reactions 👍 3 5 replies Comment options Uh oh! There was an error while loading.

Please reload this page . earthboundkid Jul 31, 2023 Yes, looking into how this works now, it’s most similar to Degit: https://github.com/Rich-Harris/degit All reactions 👀 2 Comment options Uh oh! There was an error while loading. Please reload this page . earthboundkid Aug 1, 2023 Another idea besides using gofmt or eg to rewrite is to have some system to include or exclude files that use a certain tag. For example if you say -tag client it would include files with //go:build client and exclude //go:build server.

All reactions Comment options Uh oh! There was an error while loading. Please reload this page . earthboundkid Aug 1, 2023 A little more background on my usecase: before I made Springerle, I just had a GitHub repo that was marked as a “template” so I could press the new project from template button in GitHub. I switched to making my own Cookiecutter clone because there was too much manual work in adjusting a new project to be usable.

Being able to set the module name would have been a good start, but it is also nice to setup the README and other things to follow the template. The thing I miss about the old setup was that it was a ready to use system and I didn’t need to copy stuff back to the template when I wanted to work out changes to it. All reactions 👍 1 Comment options Uh oh! There was an error while loading. Please reload this page . earthboundkid Aug 4, 2023 Copier is Python

The rest of this page is on the live site.