The way to GO!
After many years writing C# code, it is time to learn something new. And as I am running Arch Linux at home, I was looking for a language, that would run as native application on linux. Yea, I know .net Core and it works great on linux as well (See my post about TV7 with plex).
The language of choice
I had a short look at the current landscape of programming languages and I had a few criteria in my mind.
- … not be a scripting language
- … should not be run by an interpreter
- … should not need a runtime
- … can but does not have to run on ARM CPUs
- … should build native executables on Linux
- … should optionally support a GUI toolkit
With this criteria, the list shrank quite fast and the well known candidates were C and C++. The problem with them was and is that I do not like to fiddle around with memory management by myself. You can simple mess up to much. I just like to write some helper tools to make my life easier.
So I had a look at the popular languages in the TIOBE index . I just read over the list and found two languages I already heard about. The two were Rust and GO. I just headed over to the Rust and GO websites and checked them out. To be honest, GO had just simple and easy to understand code examples on their start page. A simple “Hello World!” and a few more examples. On the other side, the Rust examples on “Get started!” looked somehow too complex with to many special stuff in it.
Don’t get me wrong. Rust may be a very robust and good language. I do not doubt that. But for me it did not look like the easy to learn language.
So I went with GO.
The learning project
GO is a language with a small basic set but a good standard library. I felt quite confident to write my first app. My goal was an application to upload my local photos to piwigo without manually creating all albums and uploading the pictures. There are some scripts out there but I had no interest to mess around with them as they are quite old.
So I started messing around with some go code. I found myself in a brainf*k position. Why? Because I got conditioned for nearly 17 years to think in a “pseudo” object oriented concept where everything has to be part of a class. Just take a look at the “state of the art” stateless .net web api webservices where POCOs get passed around stateless functions that are encapsulated in classes. This is as much object oriented code as a car is a helicopter. Ok, ok, this was a bit harsh but I think you get my point. We do drift away from the real object oriented concepts. Do not forget: I still write a lot in C# and I enjoy it!
So starting without any clue how to structure an application in GO, I found out that I have to do my homework. I found a nice Github Repository with a default project layout . This is a must read for any new GO developer!
I also looked for a nice helper that would reduce my typing work to convert json API results into GO structs. I found the tool json to go that takes care of building a struct. It does not generate a 100% complete solution but close enough.
With all that I was able to write a basic application with a minimal feature set.
You can find my code mirrored on my github account and under GPLv2.
Here are some learnings from my first 20 to 25 hours with GO.
- My coding experience helped me to get into the language. But: To learn a lot I have. :-D
- After messing around with my brain struggling writing simple function without classes I found out that I am able to write “Methods” bound to a specific type.
- I also learned when to use methods and when just plain functions. In fact write a method if you modify the state of a struct. If you just use it, keep it as a normal function. As always: There might be some exceptions to that :-D
- GO does the memory management for me! A C# dev feels like being at home.
- Channels are cool but increase the complexity of the code.
- I can compile the code to a lot of different platforms. Linux, Windows and macOS or even freeBSD are possible.
- I have a native executable that is mostly statically linked.
I really enjoy this journey to extend my dev skills into a complete new direction. I definitely write more GO code in the future.
If you like to learn something new and you have some kind of Java, .net background or even scripting skills. I think GO is a good choice.
I have not yet a clue how the module system works. Jetbrains GoLand just did the work for me and downloaded the dependencies. I will do some research and check this out.
Automated testing is a open topic as well. I think there are many functions I can write some tests for. The only thing to do is to replace the Piwigo context with an interface. I will definitely have a look at that.
Building my app with my local drone.io setup should be possible. I eventually publish a docker container to dockerhub even if I prefer applications without containers on my workstation. But who knows as the uploader may run on a server on a regular basis using docker.