Creating applications with Golangspell
Golang is a great language, in my opinion, simple, fast, and focused on high performance. But, not only it can help us to create a micro-service that performs, but we can also code quite quickly.
As a PHP developer, I used to work with Symfony Framework, which could give me a big range of tools to support me to create incredible services. But there is one in specific that I liked the most, Symfony Skeleton, with a single command you can create a project and install all dependencies. It helps us to focus on the problem and quickly deliver at least a POC of the problem we are trying to solve.
As of PHP, in Go we can also create some kind of skeleton, but for that, we would need some basic structure to work on like Symfony and its architecture. Thinking of that we could use an approach to solve that problem as we are used to develop a golang application, which is the Clean Architecture. (if you don’t know what it is, you can see more here, I will not add here the benefits or why you should work with it.)
After a long time creating new applications with such structure, manually.
.
├── config
│ ├── .gitkeep
├── controller
│ ├── .gitkeep
├── domain
│ ├── .gitkeep
├── gateway
│ ├── .gitkeep
├── usecase
│ ├── .gitkeep
├── README.md
├── go.mod
├── go.sum
└── main.go
I could find a nice CLI tool called golangspell with the purpose to help us, developers, to quickly create a new application based on the previous structure on top of the Echo web framework, then we can focus on the domain.
To install it, it’s super simple
go get github.com/golangspell/golangspell
Then, to create a new project you just need
cd /path/to/new/project
golangspell init github.com/igor822/spell-test spell-test
The output will be
Loading Spells ...
Spells loaded!
Rendering template: .gitignore
Rendering template: Dockerfile
Rendering template: README.md
Rendering template: context.got
Rendering template: context_test.got
Rendering template: environment.got
Rendering template: logger.got
Rendering template: version.got
Rendering template: health_controller.got
Rendering template: health_controller_test.got
Rendering template: info_controller.got
Rendering template: info_controller_test.got
Rendering template: router.got
Rendering template: router_test.got
Rendering template: model_health.got
Rendering template: model_info.got
Rendering template: logger.got
Rendering template: main.got
Rendering template: .gitkeep
go: creating new go.mod: module github.com/igor822/spell-test
Application created!
---------------------------------------------------------
To run your application, first build it with the command:
go build
---------------------------------------------------------
Then execute the application with the command (Unix based systems):
./spell-test
Then execute the application with the command (Windows based systems):
spell-test
---------------------------------------------------------
Available endpoints:
http://localhost:8080/spell-test/v1/info
http://localhost:8080/spell-test/v1/health
---------------------------------------------------------
Find more details on README.md file
Golang Spell will provide you already a Dockerfile to containerize your application and also some common tools like logger
and appcontext
where you can “store your objects” in memory to reuse it, like a repository.
And that’s it, you don’t need to manually write a bunch of code that you need to configure router, controllers, and even the port
that your application will run. For sure it must have several new tools and improvements, like implement some util to connect quickly with NoSQL or MySQL, or even quickly installing gorm and the developer will be able to connect to a database really fast.
My idea here is to share a nice tool that I tested and was able to deliver a small service in less than a day, already deploying to production, without issues and allowing me to focus directly in my problem instead to worry about the noise of the standard code we need to write.