Golang outlook

During this year I had a chance to participate on Go programing language training. These courses are useful for golang 101, however if you would like to try it and take a snapshot, then Tour of Go is the best entry point.

The next level is creating something like working demo application, which is more complex compare to helloworld and this can be a test for tooling as well. I built a wage summary app, which can calculate the sum of salaries under managers.

Wagesum application

As you can see this is a simple CRUD application with REST interface for request. PostreSQL is the database and it will handle the long running, recursive SQL queries with go routines.

Wagesum on github

This blog entry is subjective and of course I don’t have the Philosopher’s Stone. From the horses’ mounth Go language idea popped up, when creators were waiting for long running C++ build task… All of them are well-known, iconic language designers.

Initial experiences

For me it’s like C language with Python flavour, continously watching Java/Spring Boot features. Creators tried to avoid C++ pitfalls. Lot of C++ developers will be happy to use this language, because this is a good opportunity to entry into microservice world.

Golang has got pointers and nil, but no pointer arithmetic and no operator overloading. I can live without them. Garbage collection is there, but it’s built into binary, not JVM handles that.

Golang uses static linking. As a result compiler can simplify lot of things and it can produce only one (!) executable file included everything. In addition to that dependency management could be easier, it can clean the unused libraries with a simple go mod tidy command.

Indentation and code formatting is available by default (!) with a simple gopls command. Look and feel can be the same for all developers. Error handling is different compare to modern languages’ exception handling, but you can get used to it.

Golang is not object oriented language, although it can emulate some behaviour with struct. Python looks similar for me with this: func (s MyStruct) f(p param) (RetVal, error){…}

Functional paradigm is not just supported, but recommended. It’s easy to define a function. In wagesum application emp_sal_service_test uses it, however this test class is just scratches the surface.

In the meantime it’s not a secret that C language was the ascendant, interoperability is supported, as you can read in this article. There are side effects with that, for example SQLite driver wanted to use gcc. Because of that I drop the support although light-weight, in-memory database could be useful for tests.

IMHO Convention over Configuration is slightly overused. It is okay that visibility differentiated by uppercase/lowercase first character instead of private and public, but comments in test could be asserted… It’s too much. I wouldn’t like to see business logic in comment section.

Openapi and code generation

Our favorite OpenApi generators are familiar with Golang. Thus a good OpenApi description is enough and your code is prepared. API is your first class citizen.

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i /local/api/wagesum-openapi.yaml  -g go-server  -o /local/internal/pkg --additional-properties=noservice,enumClassPrefix=true,featureCORS=false,onlyInterfaces,outputAsLibrary=true,sourceFolder=openapi

Unfortunately the generated code is not the latest and cleanest, but it can give you a good example and some hints. Nevertheless golangci/linter will start to cry sometimes.

In wagesum application you can find generated source code under internal/pkg/openapi directory. Services were moved into service package, because I had to elude circular dependencies.

Tooling

Today good programming language’s tooling is more important, then features. Golang works well in this point, lot of tidy tools are included by default, plus tons of open source libraries help the developers. Most of them are free, open source with friendly licences.

If you’ve got Java background, then gorm is alternative for Hibernate. REST interfaces can handled by gorilla mux or echo or you can write your own solution even.

I found this for project layout and I tried to follow that. Some developers stay with /src directory for sources.

Viper has been dropped, because of too much dependencies, however it is a powerful tool. I chose env, because it is a simple and zero-dependencies library to parse environment variables into structs.

Various testing frameworks were borned during years, but JUnit/Mockito is much more common and trivial to use. On the other hand Testing is by default supported and ready to use. So you can start TDD from the beginning even if you will extend the functionality with other libraries.

Go routines

Go routines are the most powerful part of this language. These are light-weight threads, which are not bounded to operating system.

Of course it’s not unique. You can mention Erlang or Zio framework in Scala. Zio calls this feature as fiber. In Java it will be included in the near future with Project Loom.

Wagesum application uses go routines in emp_sal_service ILong running, recursive queries will run parallel and processed by channels.

Summary

Go is a well designed, well supported programming language with fantastic tooling and great community. Small memory footprint, low processor consumption are trademarks of Golang. In the meantime there are many, widely used feature is missing or not there by design.

With Java background I don’t have a feeling that I must migrate all of my projects to Go. There are Scala and Kotlin and others. I can be productive with Java much more faster.

Nevertheless Golang is used widely in Kubernetes world, so it is already successful. A new language is always a new window so it’s worth a try!