The ‘error’ is coming…

Armando Alves
4 min readFeb 1, 2017

--

The first impressions about Golang by a Ruby user.

In my first post I will tell you how, after starting working as system developer using Ruby daily, I decided to try Go and which strange things I have found.

Well, 2016 was a fast year. I had worked as a system analyst since 2012, yes, UML, diagrams of sequence, software requirement specification (SRS), Enterprise Architect, etc… All of them was part of my daily life, I did not write not even one line of code during the day, only in the evenings. Since the college in 2010, when I had started to study Ruby language and the ‘community’ around it, I have created web systems with Rails for my projects, or as a freelancer, and I have gone to conferences as RubyConfBR.

Then, in July of 2016 I chose to do this “professionally” and in this company, as in most of the companies, the trendy word is Micro-Services and within it comes the phrase:

“Write a code that is easy to delete, not easy to extend”

Ok, I was writing some services in Ruby, there were others in Elixir, so why not using Go too? In college I had contact with Java, .NET, JavaScript, PHP, but some of them lacked one compiled language like C or C++, and there could be a modern compiled language, right?

My first contact with Go was the “Tour Go” from official site and after that “Go By Example”, these should be the first steps for everyone. The first book I read about Go was “Go in Action”, an amazing book but there are others such as: “Web Development with Go” or “Programando em Go (PT-BR)” and not only books, marketplaces with a lot of videos courses like “Udemy”. Wow, there is a whole world of content to study, many of them I did not list here. Well, I was felling confident to start a new service with Go ❤ !

But what would I use? Something like Rails? Or simpler as Sinatra? Well, I will follow the Go community behavior and try to keep the project with less dependencies as possible, even just standart libraries, would it be possible?

Hum, ok, before talking about package options,

The first ‘strangeness’:

With Go installed, I took a while to get used to Go environment, the variables $GOPATH and $GOROOT and the projects inside folders:

$GOPATH/src/github.com/aalvesjr/my-go-project

It was weird in the beginning, but it all worked out so fine that I did not notice the new arrangement of folders. Well, let me go back on talking about packages…

Not for a lack of choice, but this service would be just to expose two or three routes responding JSON data, then I started with package “http” and it was changed to “Gin”, yep, I tried to test other options, I needed to connect a database, Postgres probably, then I found “Gorm” without all options from Ruby ActiveRecord, but reached expectations.

Let me say that Go has a mascot, funnier than it looks:

When I was doing my first commits in Go was the time to learn in pratice the new terms: Package, Interface, Pointer, Struct, and return Errors? why?

In my project there are more than one package, I had one package to models, to handlers and I had to import them like other dependencies:

example importing a std library and one sub-package

Hum, the system is not responding as it should. With Ruby on Rails many times I call on the “console” to debug, but with Go, I have used a lot of 'log.Printf("…")' when some problems appear, I used it to debug. It is in my pipeline to try “Delve” to debug, and maybe “pop/soda” to console tests.

And to wrap it up, as a good Ruby user, I want to test it but how can I do it?

Not different from the others, the stantard library has one package “testing” to deal with this, and even some packages like “net/http” have one package support to tests itself, “net/http/httptest”. If you already are testing your Go code, you should be using interfaces and mock, right? In case you do not, read this article that helped me a lot to understand inject dependencies and the best way to use mocks.

I ended up leaving a lot of links among the text, but it is my first post about Go and I want show more code and experiences using Go in the next posts, maybe about a good practice in “functions that return errors” and you as a developer should check and deal with this return.

Comments and suggestions are always welcome.

--

--

Armando Alves

Software developer and guitar player away from the strings, spending more time with Golang and Ruby.