3 Powerful Features of Go Which I Found Bizarre

An unusual naming convention, multiple return values, and a blank identifier took me some time to fully understand

Muge Evren Bulut
Better Programming

--

Photo by Brett Jordan on Unsplash

Recently my team gained the responsibility for a small corporate API developed with the Go programming language. So we had to learn Go while we were maintaining that API. I had some fun and also a weird time trying to figure out how to adjust my mindset to fully understand what Go offers.

Go is not like the other programming languages with almost the exact syntaxes and very similar semantics. That’s why it felt so much fun to learn from scratch. Another reason I had a weirdly fun time, I had to unlearn some syntaxes and semantics from object-oriented programming and scripting languages; since those were common in the programming languages I have used throughout my career, I have realized some of those had become a habit and encoded in my brain as rules.

So I will try to explain the features of the Go programming language, which felt bizarre but amazing to me during my learning journey.

I will use two different simple examples to demonstrate with code.

The first one is this little example of creating and printing a deck. main() function in line 38 is the starting point of this program, it calls newDeck, shuffle and then print function to display created and shuffled deck.

1. Multiple Return Values

In case when you need a function to return multiple values, almost in every programming language you can find a workaround to do that.

You can return an array or an object that contains different values and use those returned values when you call the function. Even though it is not really “OK” for a function to have more than one responsibility, according to SOLID’s very first principle of “Single Responsibility”, there might be some cases where you need to use that workaround.

Go has identified that issue and has been developed with built-in functionality to support multiple return values.

As it is also very clear in the code, myValues function returns two different values. Types and order of the return values are predefined during the function definition so that, in case of any mistake within that function, the go compiler will fail to compile and throw an error.

Return and assign syntax is also very clear, simply putting a comma between the values on the return/assign statement will do the magic. Simple, but powerful!

2. Underscore ”_” Blank Identifier

Imagine if you didn’t want or need to use all of the returned values from a function, in that case, you would still need to define variables for every return value, cause otherwise go would fail on compile time. The build-in solution introduced by Go for this challenge is Blank Identifier.

You might have already realized from the examples above, there are some underscores defined where there should be variable names instead.

Range function in Go returns two variables, one of them is the index, the other is the item that we are ranging through in for loop for the slide (note: Slide is just a fancy word that is used for arrays in Go).

Since we didn’t use the index variable on that for loop, we put underscore so that go can understand that there is a variable there but I just don’t care about it and not gonna use it.

To be fair, that underscore blank identifier was the most bizarre thing, that I have found pretty hard to adjust. Because my brain had a certain plan and steps to read and understand a code block, that blank identifier was ruining the way I see a code block. So it was quite challenging for me at first.

3. Unusual Naming Convention

Using one or two-letter variable names is a convention in the Go programming language. This was the second most bizarre coding behavior, I’m still trying to adapt. I, just like any other developer learned or have been thought that we must use definitive variable names so that the code that we are writing is clean, reusable, maintainable, and understandable by others.

As you can see this shuffle function has plenty of examples of this unusual naming convention. The receiver argument in the function definition is defined with the name d shortened for deck, r in line 3 is shortened for random, and i in line 4 is shortened for index.

In my personal experience, these single-letter variables might get confusing very quickly, and cause bugs or simply ugly code blocks. But again, this naming convention is embraced and very common in the Go community.

Those features above made me feel insecure because they were a little bit intimidating at first. But currently, I’m absolutely thrilled that I have got to learn a programming language that made me feel this way and I enjoy working with Go so much. There are plenty more good features that Go offers, it might make you feel not so good at first. Just give it a chance, it might turn out to be quite an excitement.

Thanks for reading.

--

--