Learning GoLang after Ruby: What to Expect?

SumatoSoft
4 min readMar 22, 2020

You might have already heard about GoLang (also known as Go), static typed programming language created by Google. The main feature of this language is goroutines — functions able to run simultaneously ( concurrentl y) with other functions. They work much faster than regular operating system threads.

This feature makes GoLang an ideal skill to learn in the upcoming 2019, as nowadays there are a lot of highly loaded projects on the market that work with big volumes of data. Possibilities of this programming language allow to improve performance in such projects and effectively meet the challenges.

But what should you expect if you are working with Ruby and decided to take advantage of GoLang? Let me share my experience.

1. No Generic Functions

The first surprise and challenge for you as a Rubyist who is starting to work with GoLang: there are no our favorite built-in methods! There are no map, each, etc, In addition, there are no generic methods and beautiful constructions in the language.

Here is the Ruby code for the sum of all elements of the array:

<pre><code># our input data array</br>
<br>input_array.reduce(:+)</br></pre></code>

It is pretty simple! It is easy to understand, and it will not be a problem for project newcomers. There is only one line of code and 22 symbols.

And here is the code for the same function but written on GoLang:

<pre><code>// initialize variable for sum</br>
<br>sum := 0</br><br>for _, i := range input {</br>
<br>// add elements to it</br>
<br>sum += i</br>
}</pre></code>

You see the difference; and you can say: “For what do I need a language that is able to solve a simple task only with a cycle?” However, the creators of the language did not include generic functions in GoLang intentionally. Why? Because sometimes, they kill code readability: you can chain and another and one more, and each_with_object and — it becomes a little bit messy, and what is more, it forces us to make 4 passes through the array. At the same time, you can do it with GoLang with just a cycle. In this example, you can see that non-use of generic methods and functions may be for the best.

You see the difference; and you can say: “For what do I need a language that is able to solve a simple task only with a for cycle?” However, the creators of the language did not include generic functions in GoLang intentionally. Why? Because sometimes, they kill code readability: you can chain map and another map and one more, and each_with_object and reduce — it becomes a little bit messy, and what is more, it forces us to make 4 passes through the array. At the same time, you can do it with GoLang with just a for cycle. In this example, you can see that non-use of generic methods and functions may be for the best.

2. Static Typing

You might be thinking: “I must start selecting the types of data in GoLang”. However, in GoLang there is a special operator for creating new variables :=. Using this operator, you can enable the compiler to guess the type of a variable. Example:

<pre><code>func myFunc() {</br>
myBestNumber := 1</br>
}</pre></code>

It is a great opportunity to declare variables, but if you have had no experience with static typed programming languages or even Computer Science, you will need to spend some time to understand the system of types. It will help you in learning other static typed languages, like Scala, Haskell, Elixir, Java, C#. Additionally, it will be a great experience for you to see programming from a different perspective, without things like:

<pre><code>a = 1</br>
a = ‘My string, it works!’</pre></code>

In Ruby it is typical to do something like this; in GoLang you cannot do this, as it is static typed, which means that a variable can have only one type. It would be great for you to return to Ruby after Go and see the differences in your code.

3. No Classes

This is not a typing mistake, there are indeed no classes in Go. You can ask: “How can I declare my own data types?” And the GoLang’s answer is use structs. Let’s start with an example:

<pre><code>// common form of declaring your own data type:</br>
// key word “type” “nameOfType” “type to declare”</br>
type User struct {</br>
id int // list of fields</br>
}</pre></code>

What did we do in comparison with Ruby class?

<pre><code>class User</br>
attr_accessor : id</br>
end</pre></code>

After that, you can see that structs are just aggregators of simplest data types. But what if I need to add a method for this object? Should I do something like this?

<pre><code>type User struct {</br>
id int // list of fields</br>
}</pre></code>

<pre><code>func myAmazingPrintln(u User) {</br>
fmt.Println(u.id); // printing id of user</br>
}</br>
// then call it</br>
myAmazingPrint(u)</pre></code>

Hopefully, not. We do not need to do this. To add a method, you may recall the principles of programming, recall that method is just a function (in simplest examples) that has some special internal variable or inside of it. Those variables are just reference to the objects. What is the reference (reference type variable)? Comparing to the typical variable (value type variable): value base variable is variable that stores a value of certain type.
Reference type variable is a variable that stores to another location in memory
So, there is a special mechanism to define a method for the object:

<pre><code>type User struct {</br>
id int // list of fields<br>
}</pre></code>

<pre><code>// passing User by value</br>
func (u User) myAmazingPrintln() {</br>
fmt.Println(u.id); // printing id of user</br>
}<pre><code>

<pre><code>func (u *User) myAmazingModifier() {</br>
u.id += 1 // adding 1 to id of user</br>
}<pre><code>

<pre><code>// then call it</br>
u.myAmazingPrint()</br>
u.myAmazingModifier()<pre><code>

This mechanism provides you with a possibility to define “methods” in GoLang.

So, in my opinion, these are the most important changes to get used to in GoLang after Ruby.

If you have any questions, please ask me in SumatoSoft social media accounts.

Originally published at https://sumatosoft.com.

--

--

SumatoSoft

We are an IT products development company. Our team are experienced professionals who are ready to share their expertise with Medium readers.