Speed Up Go Project Development (go-katana) 🤺

July 4, 2023 ¿Ves algún error? Corregir artículo golang-wallpaper

After 6 years working as a programmer in various programming languages, I started noticing some things I missed from other languages in Go. For example, the lack of code generators that simplify the development process. This was my motivation to create go-katana, an extension with code snippets that will help you speed up your development.

What is go-katana? 👀

Go-katana is a simple extension where I collect code snippets of things I usually build daily in my Go projects, such as Repositories, Services, Value Objects, Domain Entities, Ports, etc.

The Code Snippets 🔍

Currently, Go-katana has only 4 code snippets that will help us with Go programming. However, we already have plans in the project roadmap to add more than 10 additional code snippets and a more advanced code generator that will allow us to build complete Go packages with just one command.

Generate Entities

The first code snippet is invoked with the keyword:

entity

This will generate a Go struct that will have the same name as our file but capitalized, if it's a single word, and if there are multiple words, it will separate them with "_" as is the Go convention using "Pascal Case".

Filename: admin_params.go -> AdminParams

The package name comes from the folder where the file is located

And the code it would generate would be:

./models
package models type AdminParams struct { field string } func NewAdminParams(field string) *AdminParams { return &AdminParams{ //Add properties field: field, } } func (a *AdminParams) Validate() error { //TODO: Implement return nil }

Generate Ports

For this code snippet there's not much science - it will create an interface using your file name and using your folder name as the package name.

We'll use the command:

port

It will generate the following code:

./ports
package ports type UserApplication interface{}

Generate Implementations

To achieve this, we'll add a constructor in struct form that can also return an error, and an additional line to validate the interface. Unfortunately, I haven't been able to make the import dynamic due to VSCode code snippet limitations (there's no way to get the module name automatically), so we'll have to add it manually.

We'll use the command:

impl

It will generate the following code:

./application
package application import ( ports ~/Development/work/bayonet/go-auth/application ) type UserApplication struct { field string } // Validate the interface it's completed in the struct var _ ports.UserApplication = (*UserApplication)(nil) func NewUserApplication(field string) (*UserApplication, error) { return &UserApplication{ //Add properties field: field, },nil }

Generate Value Objects

We can also generate value objects with the following command:

vo

It will generate the following code:

./application
package models type Email struct { value string } func NewEmail(value string) Email { return Email{ value: value, } } func (e Email) Equals(other Email) bool { return e.value == other.value } func (e Email) Validate() error { //TODO: Implement return nil }

Future Features 🚀

  • Package generator (Using Hexagonal Architecture).
  • Test tables generator.
  • Handler generator for different frameworks.
  • Mock generator.

Currently, go-katana is in its first version. However, we already have a planned roadmap and the project's future is to become a project generator that can be installed as an extension, providing support throughout the entire development lifecycle.

I hope you find it helpful. Here's the link to the extension:

go-katana: you can go faster

It will remain free for life for users who install it now in its initial phase.