Best way to share Go snippets and project templates

April 11, 2024
Best way to share Go  snippets and project templates

As developers, we share code snippets with each other every day. Sharing a link to go playground works great, but only for small ones that can be contained in a single file. For anything larger than that, you’d likely create a repository. Similarly to snippets, we often share template repositories that help setting up new projects in a common way.

Problems start once there’s plenty of them. Do I create a new repo for each one? This option might seem great, as it keeps things nicely separated. However, for private repositories it will require managing access to each of them.

So maybe, it’s better to keep everything in one place? That’s also possible, but someone who’s interested in only one of them will either need to clone everything and delete stuff, or use complex looking git commands like those:

    
  

Wouldn’t it be nice to have a single tool that will work for all use cases? It would, and I’ve just recently learned, that there is a dedicated tool for that in go tooling.

It’s gonew , a tool which starts a new Go module by copying another one. You can install it with:

    
  

To use it, simply run it specifying go module that you want to download:

    
  

Optionally, you can also choose a new name for the module:

    
  

Tool will automatically update module’s name to the new one.

At this point it’s also worth to mention, that it does not copy .git directory, so you don’t get git history using that tool.

It works great, but there’s small catch - it does not work with private repositories out-of-the-box.

By default, Go downloads modules through the proxy at proxy.golang.org . If the repository which contains module is not available to the proxy (which is a case for private repositories), it will attempt to download it directly from the version control repository using https. After the download, Go also verifies module’s checksum using the Go checksum database. Here’s what you need to do to change that and access private repositories.

Firstly, you need to have an ssh key configured for the git service you want to interact with. For GitHub you can find the steps in documentation . It’s used to authenticate with GitHub and accessing all repositories you have access to, so including private ones.

Once you have the ssh key configured, you need to tell git to use it instead of https for the chosen repository. To do that, run:

    
  

This command will add a new section to your global git config, which usually is ~/.gitconfig file:

    
  

You can also choose to use ssh for all repositories owned by a specific account e.g. an organization, by skipping the /repo part.

Next, you need to add the repository to the GOPRIVATE variable, which specifies repositories that go should consider private, thus not use proxy or checksum database for.

    
  

After following those steps, gonew should work for your private repository exactly the same as for any public one.

Isn’t this tool amazing? The biggest benefit I see of using it is that it allows to keep all related snippets or templates in multiple modules within a single repository. I would also love to see it becoming more popular for sharing project templates.

What do you think about this way of sharing snippets? Did you know about it? Let me know in the comments!

Go