Speed up the development of your projects in Go (go-katana) 🤺

Carlos GarcĂ­a Rosales
3 min readJul 5, 2023

--

¿Hablas español? Ver en español

After 6 years working as a programmer in various programming languages, I’m starting to notice some things I miss about 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 plugin where I collect code snippets of things I build daily in my Go projects, like Repositories, Services, Value Objects, Domain Entities, Ports, etc.

The Code Snippets 🔍

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

1. Generate Entities

The first code snippet is invoked with the keyword:

entity

This will generate a Go structure that will have the same name as our file, but capitalized if it is a single word, and if it is several words, it will separate them with “_” as is the convention in Go using “Pascal Case”.

File name: admin params.go -> Admin Params

The package name comes from the folder where the file is

And the code that would generate would be the following:

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
}

2. Generate Ports

For this snippet there isn’t much science, it will create an interface using your file name and using your folder name as the package name.

We will use the command:

port

And the code that would generate would be the following:

package ports

type UserApplication interface{}

3. Generate Implementation

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

We will use the command:

impl

And the code that would generate would be the following:

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
}

4. Generate Value Objects

We can also generate value objects with the following command:

vo

It will generate the following code.

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 table generator.
  • Handler generator for different frameworks.
  • Mock generator.

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

I hope it helps. Here is the link to the extension:

go-katana: puedes ir más rápido

This will remain free for life for users who install it from now on in its initial phase.

--

--

Carlos GarcĂ­a Rosales
Carlos GarcĂ­a Rosales

Written by Carlos GarcĂ­a Rosales

I am Software Developer working in @bayonet.io using the amazing programming language Go! I hope help you with my articles. follow me in my social medias!

No responses yet