Monthly Archives: August 2019

Learning a New Programming Language (Go language as an Example)

Summary

This post explains why and how I learned the Go language. Hopefully this will help you to learn it quickly, or will inspire you on how to learn new languages.

The Reason to Learn a New Language

There can be many reasons why someone would want to learn a new language, the main ones to me are: 1) To solve a current business problem 2) Learn concepts to adapt to current tools 3) For fun and passion. Of course, you can have a mix of these reasons to push you to learn a new language, or maybe just one strong enough of these reasons.

For a very long time in my career, C# was my main programming language, I used JavaScript a lot too, but it has always taken a back seat until TypeScript came about, and SPA became the de facto front-end development model. So for 16 years, it has been two languages and a half for me, and I have never felt the need to learn another language (Java in university doesn’t count).

Why not Haskell or F#?

When functional programming became a thing again, I tried to find the right reason to learn F# (or Haskell), but with the explosion of technical information in our industry, time became even more scarce (I have three kids under 5!) and I really needed a stronger reason to spend my time learning a new language. Unfortunately, even with @DanielChambers continuous efforts in converting me :P, I didn’t jump to the wagon.

It’s funny that the reason why I couldn’t put the effort was exactly the reason why functional programming itself is compelling; it’s the paradigm shift. The paradigm shift was so big that organisations in the I spend most of my time helping couldn’t afford to embrace it; 20+ years of OOP meant a lot of investment in education, solutions and patterns, frameworks, and staffing that made it hard to embrace such a change.

In my experience with these organisations, there might have been situations where functional languages could have solved a problem better than an OOP one, but the return of investment would have been little in the light of the legacy of these organisation.
Of course, I am not promoting that organisations should not invest in learning and adopting new technologies; that would be the path to failure! But I’m just describing the situation of most of the organisation I worked with.

This ruled out the business-need reason for me, and I am left with “learning concepts to adapt to current tools” since passion was not just enough :P. Luckily, I am surrounded by friends who are passionate about functional programming, and I managed to learn from them enough about its benefits and how to bring that to my OOP world. Conversations with these friends and colleagues like Daniel Chambers, Thomas Koster, and attending lectures by professionals like Joe Bahari, have helped me a lot in adopting functional concepts to my C#.

I Found The Reasons in Go

Gopher

So I stayed on two languages and a half, until last year when I got the chance to work on a project in which we used Kubernetes. Once you step in the Kubernetes world you will realise that Go is the hero language; Kubernetes is written in Go, Helm is written in Go, and the templates Helm uses is based on the Go template engine. Although you can use Kubernetes without learning the Go language, once you want to get a little deeper it feels that learning Go would be an advantage.

In addition to that, with Cloud being my main interest, I have been seeing Go used more and more as the language of choice for many of the cloud-native project, products and services.

During the same time, many of my colleagues and Twitter friends have been porting their blogs from database-driven engines like WordPress to static website generators like Jekyll. I have two websites that could benefit from that, 1) my blog emadashi.com 2) and dotnetarabi.com podcast, which I built on ASP.NET and Subsonic ORM of Rob Conery’s. My friend Yaser Mehraban kept teasing me and applying his peer pressure until I surrendered, and I finally started looking into it moving my blog and my podcast to a static website generator.

My choice was Hugo; to me, it seemed the most mature static site generator with the least amount of churn and learning curve. And guess what, Hugo is written in Go! And the templating engine is based on Go’s. Same as Kubernetes, you don’t need to learn Go if you want to use Hugo, but it’s just another compelling reason to be familiar with the language.

So by now, it feels I am surrounded by problems that are being solved with Go, and it’s more evident that there is a greater possibility for me to work in Go in the future, even professionally.

All this, in addition to the low barrier of entry due to familiarity with C#, encouraged me to jump to the waters.

Where did I Start?

There are so many ways a person can start learning a language, to me I wanted to learn the language fast and learn just enough to get me going. For this reason, I didn’t pick up a book that would take me a while to learn, even though a book is probably the most profound way.

Instead of picking up a book, I went to https://golang.org and checked what the website has to offer; most of the modern projects and languages have documentation that includes tutorials and Getting Started guide. If these guides are well crafted it would be a great learning boost, and to my luck Go had great content.

Set-up

The first thing I wanted to do is to set up the environment and run the most basic example (the hello world of Go), for that I followed the Getting Started guide. Setting up the environment as a basic step for learning a language is very important; it will give you an understanding of the requirements of the language and will set up some expectation on how friendly the experience is to you, it breaks the ice. Also, it paves the way to the Hands-On step coming soon; I will explain this step later in this article.

Foundation

Now that my environment is setup and I ran my hello world example, I needed to understand what is really going on: how the code compiles, how it runs, how it is packaged, how it is hosted; I needed the foundational concepts to establish a firm ground to base my learning on. Learning the syntax and the various Go features will come along, and it will take time, but you can’t postpone the foundations. For this, I followed the “How to Write Go Code” guide. The article’s title might not sound too foundational, but the content lays the concepts.

Cruise as you need

If this is NOT your first programming language to learn, then you are already familiar with the concepts of structure flow: functions, loops, if clauses,…etc. This gives you a very good advantage to sweep through these swiftly; it’s unlikely that these are too different from other languages. A fast run through should be enough to capture anything standing out.

For this I used the Tour; there are two great things about the tour: 1) it has a simple navigatable structure 2) it is associated with an online playground where you can experiment and confirm your understanding on the spot. There is a wide range of topics covered in the Tour, some of which I would go through fast, and some I would take my time to comprehend; e.g. Slices can be little confusing compared to arrays in C#.

Note: Everyone’s experience is different, so it will not make sense to list the topics I went through swiftly and the ones I spent time on, use your own experience to judge that for yourself.

As for the advanced topics I left out a little until I had a better grasp on the basics of the language; overwhelming yourself with advanced topics at this stage might have a counter effect on your learning.

Hands-On

After understanding the basics from the How to Write Go code, and sweeping through the Tour, it was time to have my hands on the language; this is the only way you can really understand and learn a language.

I needed a problem to solve so I can have a driving purpose. The problem I chose is to import the existing records of DotNetArabi from the database (guests and episodes) to create corresponding Markdown files for the Hugo website, so this was my first program.

It’s important to understand here that I wasn’t 100% on top of things yet (neither now :P), but it was the practical experience that I relied on to grasp the concepts and gain the experience. If you leave the practical side for too long you will find yourself forgetting the basics, or that you are learning too advanced topics that you will rarely use. An iterative approach is very good here.

So I gradually built the application; each time I am stuck I’d either refer back to the Tour, or google it if it is not covered there (e.g. connecting to a database). In each of these stuck-and-solved situations, I take a moment to make sure I understand the solution and the technique behind it. Copy and paste is absolutely fine as long as you pause and comprehend.

Advanced Topics

Ok now at this stage I feel like I know the basics, and I am comfortable writing a program without big issues. But at this stage, writing a program in Go would give me very little advantage (if any) over writing it in another language; I am not getting the best out of the language. It’s the advanced features that make the difference, things like goroutines and channels by which we achieve concurrency with the least amount of maintenance overhead.

Don’t be afraid of the advanced topics; avoiding it the advanced topic because they might be complicated will jeopardise the value we are getting from learning a language in the first place!

So for this, I continued the Tour above for the advanced topics. The playground was of tremendous value as you will need to change things around to confirm your understanding. Also, the Tour has some exercises that will poke your thoughts, I highly advise trying these out! This will not just push you to comprehend the concepts, but it will also expand your horizons for the use cases that you might need these advanced features.

It would be great fun and value if you can go back to your pet project and try to implement some of these advanced concepts, and this is what I did. I went back to my application and utilised goroutines to extract the data to the markdown files.

Unit Testing

Leaving unit tests to the end wasn’t undermining their value, rather I wanted to focus on the language itself first; test frameworks and push the complexity and the learning curve high enough. My experience from JavaScript stings until now :P.

The Best of Go

Finally, Go website has a section called “Effective Go“. This section is not really a referential documentation, but it can be very valuable so that you write the Go code as the language has intended it to be like. It provides further context and rounded styling to writing the language in the best form.

I also here advise to pick and choose the topics, reading the whole thing might be counter-productive.

Close the Loop, Complete the Picture

By now you’d think you finished, but this is just the beginning; now is the time to tie things together by revising the language’s main characteristics, philosophy, and the greatest advantages.

If we look specifically at Go, as our example, this might be things like the simplicity of Go, where there no classes, no inheritance, or generics. Or things like concurrency and how Go deals with State in asynchronous code execution. At this stage, it will be valuable to check the videos, like Sameer Ajmani’s talk, and the literature out there that discuss “Why Go“.

I also found the FAQ in golang.org a valuable resource for some of the justifications and explanations. You should not read this as an article though, pick and choose the topics of interest.

But isn’t this backward? Shouldn’t I learn about these things at the beginning? True, you can learn these at the beginning, but you will not value the claims until you try and put your hands on the problem in practice, until then it will be merely claims in the air. So even if you start with these, you should also revise them and make sure you tie the loop.

Conclusion

In my journey to learn Go, I did the following:
• I had a good reason
• I established the core concepts
• I installed the tools and ran the “hello world” program
• I scanned through the structure flow
• I put my hands on the code and wrote the first program
• Read the advanced topics, and used the playground to confirm my understanding
• Watched more videos on why to use Go and its advantages

It’s important to say here that choosing a language to adopt for in an organisation involves more than just learning it. If you are in a position to influence a decision just be mindful of that.

I hopes this helps you out, enjoying coding :).