Databases, DevOps, Software development

Golang for Rubyists. Part 7. Ruby and Golang, methods comparison

Hello, my dear friends. We all love Ruby (you too, right?) for its expressiveness and a set of useful methods out of the box. It would be a pleasure if when start using a new language, you had the similar methods or at least familiar ways to achieve same goals. Let’s try to pick a few useful methods from Ruby and find out, are there any equivalents in Golang for them.

Not so short and expressive, huh? But still pretty straightforward. The only confusion I can anticipate is the last example, with pop and unshift. Just to explain, at first we assign to x the last element of an array and we modify the array variable to be everything starting from the element at index 0 to the pre-last one. And at the next line we create a new slice with just x and append an array to it.
Also, you could notice here, that usage of [] to access an array/slice element is exactly the same as in Ruby (or most of the languages).

Here we iterate through a collection, without original collection modification, then we reject some value also without a modification, then we get a random element from it, then we have a basic min/max methods and a shuffle method, which shuffles an array elements. Are you optimistic about Golang abilities here? Let it a try! Nope, let me just tell you, that Go is not a functional language. Not at all. Not for an iota. The only way you can achieve map/reduce/filter functionality is by writing for loops. Also, there are no built-in min/max functions, unfortunately. Surprisingly I was able to find a shuffle implementation:

As you can see, this will work only with slices of integers. In the link above you can find a more generic implementation. It uses reflection mechanism of Golang, which we didn’t explore yet.
So, this function may look cumbersome, but let’s take a look onto Ruby’s map method under the hood:

Looks pretty similar, isn’t it? Sure, in Ruby we don’t work with types – that’s the only difference. So, long story short, use for loops, that’s the way to go.
But also one thing I want to mention is that there is a more convenient way to iterate through a collection, here how it looks like (works for both maps and arrays):

Hashes

One of the most heavily used data structures in Ruby is Hash.
Just kiddin, I have no idea, which is the most heavily used one. Let’s take a look at these examples, where we apply a couple of useful functions to it:

Bang-bang! Such a bliss! Actually, Golang has a pretty extensive set of methods for strings manipulation, you can find way more in their official documentation: https://golang.org/pkg/strings/

Conclusion

There are much more to discover, so start using Golang for your pet projects or for your actual projects at work, f.e. if you plan to have some simple Lambda function, it’s a good place to introduce it, check out my past blog post on how to make it painlessly.
But for today that’s it, thank you for reading.
If you don’t want to miss the next posts, subscribe to my Twitter, tho I chat there for random topics, also I share some interesting posts I’ve discovered, as well as my own.
And psst, there is a big orange button beneath, using it, you can buy me an espresso macchiato, which I really love and will appreciate 😉

4 thoughts on “Golang for Rubyists. Part 7. Ruby and Golang, methods comparison”

Hello, I’m also a Rubyist and now trying to learn Golang (still a rookie). I appreciate the job you’ve done. May I repost your articles in my blog? For sure I will post your original link at the top of the article. Look forward to your reply. Thanks!

Hi Konstantin,
You’re totally right, the official language name is “Go”. However, “Golang” is not only a keyword and the domain name. It is also a Github repo name and Reddit’s subreddit name. Ruby, Java or others are never referenced as *lang, but it’s not the case for Go. I can assume that it happens due to the succinct naming, which can be misleading, as a word in general and as a name in particular (I mean the confusion with Go! language).
However, thank you for emphasizing that. I think at least it makes a perfect sense to use the proper language name in the title.