Do not use go mod init anymore

Deniz GÜRSOY
6 min readNov 6, 2022

Introducing a new project starter tool for developers

Whenever I need to create a new Go project, I follow the standard project structure. So I initialize my project by creating cmd, internal, and pkg folders. These days, we always run our applications in containers with orchestrations tools. Therefore; I also create Dockerfile and Kubernetes deployment files from scratch for new projects that I create. I was planning to automize this project creation process and I decided to create a CLI tool with Go that help me do that.

Taken from https://qqq.ninja/blog/post/go-structure/

But what should such a tool achieve?

First of all, in the IT industry projects actually go on how they are started. For example, if you start with a good/correct project structure, developers will follow that project structure. If not, presumably it is going to be a mess. So this tool should be able to copy some project structure that people agreed on.

Taken from https://www.linkedin.com/posts/javascript-developer_activity-6993132590106824704-TbBH

In companies, there are some common files with small differences in every project such as CI/CD configuration files, containerization files or deployment files. Therefore, the tool should allow its user to customize some parts of the project created.

Developers also prefer different libraries in their projects. For example, some developers prefer third party logger such as zap, zerolog, and logrus while some stick with the built-in logger. Every developer has a different preference for web framework - echo, gin, gorilla, etc. So, this tool should allow users to select libraries they want to use for different categories.

Gotouch

I started Gotouch project to meet the criteria mentioned above. This started as a hobby project to learn and practice Go. In the beginning, it aimed to create only Go projects but then it became a generic tool for managing projects or files. Gotouch can be now called as a templating tool for IT.

Gotouch is inspired by technologies Helm, Maven and Vue CLI. Gotouch helps users to maintain project structure like maven archetypes, allows users to modify project content with values just like generating Kubernetes YAMLs with Helm and finally, it allows users to customize the project to be created by asking their preferences.

Taken from https://ro.pinterest.com/pin/844917580080076633/

The crucial thing about Gotouch is that it is customizable. You can create your templates and distribute them among your friends and colleagues. The suggested way is to host your files inside a git platform and use the raw URLs of the files.

How to use Gotouch

Firstly, install gotouch.

go install github.com/denizgursoy/gotouch/cmd/gotouch@latest

Then execute gotouch with no argument. It will display default project structures. In the default properties file, there are two Go projects. First one is a simple module project with no directory, it has only ‘main.go’ and ‘.gitignore’. The second follows the standard project structure.

When we select either of the choices, it will ask for module name, and whether you want Dockerfile, Kubernetes deployment files, and Makefile.

When we make the selections seen above, gotouch will create the following directories and files in the working directory.

`-- my-app
|-- Makefile
|-- build
| `-- package
| `-- Dockerfile
|-- cmd
| `-- my-app
| `-- main.go
|-- deployments
| `-- app-deployment.yaml
|-- go.mod
|-- internal
`-- pkg

Go.mod will be created with the current Go version and module name user entered.

How to customize

Let’s say you have three different kinds of project structures in your company. For example, you might be using Python for your BFF’s, Java for microservices, and Go for API gateways. In this example, we will focus on creating a project with Go.

You can write your properties YAML and pass the address of the YAML to gotouch with the -f flag. Properties YAML contains a list of project structures. A project structure must have a name and URL to the template ( it can be a git repository or a tar.gz archive file created by Gotouch package command ). If the project is a Go project, you need to set the language to ‘go’ or ‘golang’. If the language is set to ‘go’, gotouch will check whether go binary is installed on the machine, create/modify go.mod in the project, and be able to add dependencies.

When gotouch is executed with the -f flag ( it can be URL or local path), it will display all the project structures in the YAML.

gotouch -f path-to-properties-yaml

After a project structure is selected, Gotouch will ask for the module name. A project structure can have a list of questions allowing the user to customize their projects. Gotouch will prompt every question in the selected project structure. The user is expected to select a choice. The selected choice can create new files, add new dependencies, and can add new values for templating.

The question will be prompted like:

If Gin choice, for example, is selected, gotouch will add all the dependencies listed under the gin choice to go.mod by calling go get . It will also create a ginHandler.go under the handler directory with the content provided. If the directory does not exist, it will create the directory. Finally, it will walk through files for templating with the chosen values. Take the following code as an example.

It will be rendered to this:

Managing Dependency in other languages

If language of project structure is set to go, every dependency has to to be string. For other projects, you can use any format in your dependency. For example, in a Maven project, you can define your dependencies as object as seen below.

Gotouch merges dependencies of all selected choices and add them as an array to values with “Dependencies” key so that you can template with dependencies.

If the user select Postgres choice, pom.xml will be generated like:

For more information see the repository on GitHub.

References:

--

--