Golang & C#, are they so different?
C# is dead
Long live Golang.
Golang won’t stand
C# will last…
How many times we see posts/articles to compare and pick difference between <Placeholder> language and <Placeholder> language.
This article is also a comparison, but with less opinions, and more material to give you your own understanding.
I’ve decided to implement three different small programs using C# and Golang, following their idiomatic way, obviously we can talk which way is idiomatic, but lets keep it simple:
- Error Handling
- Communication between objects within one program (threads task etc.)
- Pipelines contextual data carryover.
Why I picked those? I think I found myself in too many situations when I thought this is big problem for my program, and it doesn’t touch actual business logic at all.
Error Handling
Errors happens. Assuming happy path is wrong. I think we can agree on that.
On left I present recommended Golang program and C# counterpart on right. At first glance you can spot a lot of if’s in Golang version. Without proper IDE code folding it would be nightmare to work with it. But after folding we get :
Clearly you can see that some operation return possible errors, and there is a procedure to handle it. Moreover error handling is treated as normal “happy path”.
C# uses classic try catch pattern. And execution is divided into two parts.
- TRY — try aka. happy path
- CATCH — small procedures in case of error.
My opinion: “Both approaches are OK. I think despite difference between languages both of them helps you to handle your errors as they appear in clean way.”
Communication
Multi threaded/task programs is here to stay. We need to design our logic in a way that communication is just way we organize business logic not reason to do it such way. When comparing both languages I had a problem which C# strategy to use. There are bunch of them.
I picked
- Golang : goroutines + channels
- C# : event based communication
Golang shines with its goroutines and channels approach. Sharing via communication, not locking object is one of reasons it is so popular language right now.
Event based communication in C# requires more boilerplate code, but still you can get decent result.
My opinion: “ Golang decided to make big change in communication, do it once and do it right way. C# has its legacy, before picking solution do your research and pick right tool”
Pipeline context
Pipelines are everywhere. We declare execution in form of linked strategies, decorators etc.
Golang has Context package which is used all over place to pass contextual information, HTTP context, cancellation requests, timeouts etc. It is very generic in its nature, so it can be used in many situations.
ASP.NET has HttpContext Class (System.Web) | Microsoft Docs which is one of fundamental building blocks of middleware ASP.NET pipelines. But it is not so generic and it would be misleading to use it in console app. What I can think of is basic idea of Dictionary.
My opinion: “C# HttpContext is great and has a lot of features, when you are Http. If not, only solution is something custom. Golang has context package. It is quite hard to grasp its idea at start. And I suspect it violates Single Responsible Principle, lets be honest this is C# CancellationToken + ImmutableDictionary.”
That’s all.
Code can be found :
Happy coding!