GoLang vs. Ruby: What to Expect?
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 (concurrently) with other functions. They work much faster than regular operating system threads.
This feature makes GoLang an ideal skill to learn in the upcoming 2024, 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:
# our input data array
<br>input_array.reduce(:+)
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:
// initialize variable for sum
sum := 0
for _, i := range input {
// add elements to it
sum += i
}
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.
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:
func myFunc() {
myBestNumber := 1
}
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:
a = 1
a = 'My string, it works!'
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:
// common form of declaring your own data type:
// key word "type" "nameOfType" "type to declare"
type User struct {
id int // list of fields
}
What did we do in comparison with Ruby class?
class User
attr_accessor : id
end
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?
type User struct {
d int // list of fields
}
func myAmazingPrintln(u User) {
fmt.Println(u.id); // printing id of user
}
// then call it
myAmazingPrint(u)
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 this or self 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 ADDRESS to another location in memory
So, there is a special mechanism to define a method for the object:
type User struct {
id int // list of fields
}
// passing User by value
func (u User) myAmazingPrintln() {
fmt.Println(u.id); // printing id of user
}
func (u *User) myAmazingModifier() {
u.id += 1 // adding 1 to id of user
}
// then call it
u.myAmazingPrint()
u.myAmazingModifier()
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.
Let’s start
If you have any questions, email us info@sumatosoft.com